How can I set a variable in another object?

My enemy needs to be spawned when the variable spawned for that enemy is set to true. However, I want another object (the thing I collect) to set spawned to true. I’ve looked around and so far I haven’t been able to figure this out. I’ve been trying to use GetComponent(scriptname) to set it, but I haven’t been successful using that.

I feel like this should be really simple, like this…
spawnOb.spawned = true;

but that doesn’t work, and trying this…
spawnOb.GetComponent(scriptname).spawned = true;

doesn’t work either. Can someone help me figure this out? I’m using C#

@dreammix, Hi!

From what I understand you want to spawn in an enemy when you pickup a collectable? Specifically, you want to set a variable “Spawned” in the enemy script to true when you pick up the object, then when that variable is true you want it to spawn?

It’s probably possible to do it the way you want providing the enemy is already spawned and it’s actually only disabled. However if that wasn’t the case you can’t set a variable of a script that doesn’t even exist in the game (not instantiated).

A more effective way would be to not even have a variable on the enemy and instead use the Instantiate function to clone a prefab of the enemy when you collect your pickup. Simply create a public GameObject variable, and set it to your enemy prefab from the inspector. Then use that variable when you call the Instaniate function:

If I mistaken the question or you need further help just ask :slight_smile: