- Home /
Why can't GameObject.Find find more than one object at the start function?
Okay so i'm setting up this real quick 2d game where you can only move left and right and catch objects falling from the top of the screen. i decided to add 3 hearts at the top right side of the screen and everytime you miss an object one heart dissapears. In the "Spawnables" Script applied to all the different prefabs that fall from the sky, i declared a float variable called objectsMissed, now everytime an object reaches -6 on the y axis it will be destroyed since it's not on the screen anymore and we add 1 to the objectsMissed variable. The problem is these objects are prefabs and you cannot assign gameObjects to prefabs so i have to find them through script at the start function, the first heat(furthest right) dissapears when i first miss an object, but when i miss a second it gives me this error(NullReferenceException: Object reference not set to an instance of an object). Here's my code.
public float objectsMissed;
private GameObject heart1;
private GameObject heart2;
private GameObject heart3;
void Start()
{
objectsMissed = 0;
heart1 = GameObject.Find("HeartR");
heart2 = GameObject.Find("HeartM");
heart3 = GameObject.Find("HeartL");
void Update()
{
if (transform.position.y <= yBounds)
{
Destroy(gameObject);
objectsMissed++;
}
if (objectsMissed == 1)
{
heart1.SetActive(false);
}
else if(objectsMissed == 2)
{
heart2.SetActive(false);
}
}
Answer by Hellium · 3 days ago
Having the falling objects handle the hearts is not the way to go.
You need to rework the architecture of your game. Suggested architecture:
CODE NOT TESTED
1. A Life
script, placed in your scene, handling the hearts
public class Life : MonoBehaviour
{
// Drag & drop the hearts in the inspector
[SerializeField] private GameObject[] hearts;
private int amount;
public int Amount => amount;
private void Awake()
{
amount = hearts.Length;
}
public void Decrease()
{
if(amount == 0) return;
hearts[amount - 1].SetActive(false);
amount--;
}
}
2. A Spawner
script, placed in your scene, handling the spawn of your objects. You should already have something similar
public class Spawner : MonoBehaviour
{
// Drag & drop in the inspector the prefab to spawn, replace `Spawnee` by the name of your script
[SerializeField] private Spawnee prefab;
// Drag & drop the object with the Life component in the inspector
[SerializeField] private Life life;
private IEnumerator spawnCoroutine;
private void Start()
{
spawnCoroutine = Spawn();
StartCoroutine(spawnCoroutine);
}
private IEnumerator Spawn()
{
WaitForSeconds wait = new WaitForSeconds(5);
while(true)
{
yield return wait;
// Consider using a pool instead of destroying and instantiating
Spawnee spawnee = Instantiate(prefab);
spawnee.Despawned += OnSpawneeDespawned;
}
}
private void OnSpawneeDespawned(Spawnee spawnee)
{
spawnee.Despawned -= OnSpawneeDespawned;
life.Decrease();
if(life.Amount == 0)
StopCoroutine(spawnCoroutine);
}
}
3. A Spawnee
script, placed on your prefab you instantiate. Basically the script you have in your question
public class Spawnee : MonoBehaviour
{
[SerializeField] private float yBounds;
public event System.Action<Spawnee> Despawned;
void Update()
{
if (transform.position.y <= yBounds)
{
Despawned?.Invoke(this);
Destroy(gameObject);
}
}
}
@Hellium i can't reference the spawnee script cz there are 4 spawnable items, in my game manager script(the spawner) i created a list and placed all the 4 items in it so they spawn randomly, is there a way to call the spawnee scripts even if it is attached on 4 different prefabs?
You need to tweak the Spawner
according to your needs. Since you haven't provided your spawner code in the first place, I had to go with a simple version of it, but I'm sure you'll figure how to merge my spawner script into yours ;)