Hi, I am trying to enable a script from another object. I managed to enable/disable a script in the same object with
GetComponent<test> ().enabled = true;
Then I tried it with the script in the other object with this:
public GameObject red;
void Start()
{
red.GetComponent<EnemyMove> ().enabled = true;
}
I assigned the other object to ‘red’ in the inspector and I don’t get any errors but the script doesn’t get enabled. I am pretty new to programming, so sorry if it is something obvious but I can’t see it and a google search didn’t help.
Edit:
I tried some things I found via google:
GameObject.Find ("Geist_rot").GetComponent ("EnemyMove").enabled = true;
gives the error: “Type UnityEngine.Component' does not contain a definition for
enabled’…”
GameObject.Find ("Geist_rot").GetComponent (EnemyMove).enabled = true;
gives: Expression denotes a type', where a
variable’, value' or
method group’ was expected
The best overloaded method match for UnityEngine.GameObject.GetComponent(System.Type)' has some invalid arguments Argument
#1’ cannot convert object' expression to type
System.Type’
red.GetComponent ().enabled = true;
gives: The type arguments for method `UnityEngine.GameObject.GetComponent()’ cannot be inferred from the usage. Try specifying the type arguments explicitly
Finally found something that worked:
GameObject varGameObject;
void Start()
{
varGameObject = GameObject.Find ("Geist_rot");
varGameObject.GetComponent<EnemyMove>().enabled = true;
}
I am not really sure why this works and the first one doesn’t. The line that activates the script is the same. The only difference I see is that in the first version the other object is assigned to the variable in the inspector and the variable is public and in the last version it is assigned via the first line in Start and the variable is private, right? Public/private doesn’t matter, I tried changing my variable from the first version to private and it changed nothing. So why does the one work and the other not?