- Home /
Can't change variable in script cia "GameObject.Find" (C#)
I'm a begginer in scripting (I'm write in C#) and I'm making a scene in which there is a spawning manager which controls enemy population and when it drops below maximum, it spawns new enemies over time. Now what I need to do is to write a script which will be attached to enemies and decrease variable of population by one when enemy dies, but I can't figure out how to do this. My testing script looks like this:
public class EnemyScript : MonoBehaviour {
public float hp = 100f;
public Component ManagerSript;
void Start ()
{
ManagerSript = GameObject.Find("SpawningManager").GetComponent<ScriptAttachedToManager>();
}
void Update ()
{
if (hp < 0){
//Other dying stuff
//Here I want to decrease float variable called "EnemyCount" in script attached to the manager, when I delete this line there are no errors
ManagerSript.EnemyCount--;
}
}
}
But when I save this script I get this error: Assets/EnemyScript.cs(22,38): error CS1061: Type UnityEngine.Component' does not contain a definition for
EnemyCount' and no extension method EnemyCount' of type
UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?) Also, without the last line there are no errors. I have no idea what is wrong here. Please, help me.
Answer by Eric5h5 · Jun 08, 2014 at 05:06 PM
Use the actual type (which is the name of the script), not Component.