Enable Script from another Object (C#)

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?

I don’t see anything that would prevent EnemyMove from being enabled… Unless the gameObject is deactivated?

You should also probably do this if you just care about the EnemyMove script and don’t need the red GameObject for anything else.

public EnemyMove red;

void Start() {
  red.enabled = true;

  // Test to see if the gameObject is not active
  Debug.Log("EnemyMove script enabled: " + red.enabled);
  Debug.Log("red.gameObject.activeInHierarchy: " + red.gameObject.activeInHierarchy);
}

I think I am getting crazy now. I changed nothing, I just removed the // to test the first version once again and it did work. I changed nothing in the script, and don’t know why it wasn’t working before.

But regarding your suggestion: The ‘red’ in the first line is the name of the variable, right? How would my script know which instance of the EnemyMove script it should activate? I assigned the same script to different enemy types and I want to disable them independently from another.

In your original script, you had this.

public GameObject red;

I assume that you were assigning “red” in the inspector, yes?

// Assign in inspector.
// Gets us a reference to the GameObject.
public GameObject red;

void Start() {
  // Get a reference to the script through the gameObject.
  EnemyMove redMoveScript = red.GetComponent<EnemyMove>();
  // Now we can do whatever we want with the script.
  redMoveScript.enabled = true;
  redMoveScript.SomeMethod();
}

Well, you can assign GameObjects in the inspector, but you can also assign scripts attached to GameObjects in the inspector.

// Assign in inspector
public MoveScript scriptOnRed;

void Start() {
  // We already have access to the script.
  // No need to go through the GameObject
  scriptOnRed.enabled = true;
  scriptOnRed.SomeMethod();
}

If you don’t really need the GameObject directly, removing the step of going through the GameObject to get your script means you get cleaner, easier to read code.

This may be a dumb question but I assign objects in the inspector by dragging them from the hierarchy to the inspector. How can I assign a script that is attached to an object in the inspector. Dragging it from the project browser would assign the script itself, not its instance, right?

You can drag the game object with the script on it from the hierarchy to the inspector, and it totally works!

You can also assign from a prefab (not the same as the script!) in the project window, but then you have a handle to the prefab and need to Instantiate it before you can use it in game.

public MoveScript moveScriptPrefab;

void Start() {
  MoveScript instantiated = Instantiate(moveScriptPrefab) as MoveScript;
  instantiated.enabled = true;
  instantiated.SomeMethod();
}

And what if this gameobject has mor then one script?

If it’s the same script added multiple times, I’m not quite sure how to tell them apart unless you put a public string in them or something to identify them.

If it’s different scripts, then you can either get a handle to the GameObject then GetComponent<>() to get the script you want, or have a handle to both scripts.

// Assign in inspector
public GameObject anObject;

void Awake() {
  someScript = anObject.GetComponent<Script1>();
  anotherScript = anObject.GetComponent<Script2>();
}

Script1 someScript;
Script2 anotherScript;
// Assign the same game object to both
public Script1 someScript;
public Script2 anotherScript;

void Awake() {
  if (someScript.gameObject == anotherScript.gameObject) {
    Debug.Log("someScript and anotherScript are both on the same gameObject named " + someScript.name);
  }
}

Oh sorry, my fault. You declare the variable with

public NameOfScript nameOfVariable

so you tell the script which script it should use with ‘NameOfScript’ Everything clear now, thank you.