Script makes more than one clone of landing area prefab

As the topic says the script is making more than one clone och landing area so i have about 10-15 flares.
Here is the player script if anyone can help me. using Unity version 5.3.8p2.

public Transform playerSpawnPoints; // The parent of the spawn points
public GameObject landingAreaPrefab; 

private bool reSpawn = false; 
private Transform[] spawnPoints;
private bool lastRespawnToggle = false;

// Use this for initialization
void Start () {
	spawnPoints = playerSpawnPoints.GetComponentsInChildren<Transform> ();
}
// Update is called once per frame
void Update () {
	if (lastRespawnToggle != reSpawn) {
		Respawn ();
		reSpawn = false;
	} else {
		lastRespawnToggle = reSpawn;
	}
}

private void Respawn() {
	int i = Random.Range (1, spawnPoints.Length);
	transform.position = spawnPoints [i].transform.position;
}

void OnFindClearArea () {
	Invoke ("DropFlare", 3f);
}

void DropFlare () {
	Instantiate (landingAreaPrefab, transform.position, transform.rotation); 
}

Hi @AntonJ,

Looking at this code, both of the booleans are set to false, then in this code;

	if (lastRespawnToggle != reSpawn) {
		Respawn ();
		reSpawn = false;
	} else {
		lastRespawnToggle = reSpawn;
	}

…I can’t see how the Respawn method actually ever gets called, as the first time you step into the Update method, both of the boolean values are the same, false, so you would go into the else code block, where you then set a boolean lastRespawnToggle = reSpawn - both of which are still equal to false. This would just keep happening indefinitely.

Could there be other instances of this script attached to other GameObjects, or any of this code else where in your project where the boolean flags are different?


Updated Tue Nov 14 2017 10:51

Looking at the project files on GitHub, perhaps it’s not this script that is the issue, it seems that there is a message being sent OnClearAreaFound which would call the method in the code you have posted, which in turn calls DropFlare.

How does your ClearArea.cs script compare to the following;

using UnityEngine;
using System.Collections;

public class ClearArea : MonoBehaviour {

	public float timeSinceLastTrigger = 0f;

	private bool foundClearArea = false;

	// Update is called once per frame
	void Update () {
		timeSinceLastTrigger += Time.deltaTime;

		if (timeSinceLastTrigger > 1f && Time.realtimeSinceStartup > 10f && !foundClearArea) {
			SendMessageUpwards ("OnFindClearArea");
			foundClearArea = true;
		}
	}

	void OnTriggerStay (Collider collider) {
		if (collider.tag != "Player") {
			timeSinceLastTrigger = 0f;
		}
	}
}

Specifically, the Update method.

1 Like

Thanks for the help i found the problem, i only had write if (timeSinceLastTrigger > 1f && Time.realtimeSinceStartup > 10f) in the clear area script.

1 Like

Great to hear you’ve solved it and can move forward, must have looked interesting with all the flares though! :slight_smile:

Looked pretty bright i can tell you. :grin:

1 Like

Lol… I bet! :smiley:

Privacy & Terms