- Home /
How to activate a specific script in a Prefab.
using UnityEngine;
using System.Collections;
public class playselection : MonoBehaviour {
public Component moveScript;
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
moveScript.enabled = true;
}
else
{
moveScript.enabled = false;
}
}
}
I have been trying to active a specific script that is located within the object (cube move) but for whatever reason it is not letting me do that. This is the script that is on the GUI texture, i want it so that once the GUI text is pressed, the cube move script is activated and the controls of the player resume.
Once they have resumed, i need the gui text to delete (destroy).
Help, please!
Answer by robertbu · Oct 07, 2014 at 11:16 PM
There is no 'enabled' property in a component. Usually the the hierarchy is:
Object-->Component-->Behaviour-->MonoBehaviour-->YourScript
The enabled flag is in Behaviour class, so you can make 'moveScript' of any of the types above component. Typically I see either MonoBehaviour, or YourScript.
Could you explain a little further? I get what you are saying, but how would i go about doing it? Putting the script into an empty gameObject?
Based on your comment Chemo, maybe we are going about this the wrong way. Based on your example code, I assume you had a script on a prefab. And once that prefab is Instantiated() into the scene, you wanted to enable that script. But maybe what you want to do is enable the game object. If so, then 'Component' should be GameObject, and the call should be moveScript.SetActive(true).
Note both my original answer assumes that the above script is on some object in the prefab, and that moveScript has been initialized by drag and drop. Note the above script must be on an active game object. A script does not Update() messages when the game object is deactivated.
Answer by Cherno · Oct 07, 2014 at 11:31 PM
If by "Prefab", you mean an instantiated gameObject that is actually in your scene:
Change the word "Component" in line
public Component moveScript;
to the actual class name of the moveScript. So, if your script has the name MoveScript.cs, the line would be:
public MoveScript moveScript;
I did that, but it doesn't do anything. It doesn't activate the Cubemove script that is located on another object.
using UnityEngine;
using System.Collections;
public class playselection : $$anonymous$$onoBehaviour {
public Component moveScript;
// Update is called once per frame
void Update()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
moveScript.GetComponent<Cubemove>().enabled = true;
}
}
}
The moveScript of course HAS to be the $$anonymous$$oveScript script that is on the gameObject in question, it can't just be a script on a Prefab that sits in the project folder. Just to be clear because I don't know what you mean by "another object".
Also, your script enabling code is wrong now.
moveScript.GetComponent<Cubemove>().enabled = true;
would mean that the moveScript has another Component on it called Cube$$anonymous$$ove... which it doesn't because it is itself a Component on a gameObject.
The line has to be
moveScript.enabled = true;
Just as before, but you have to change to initial declaration to what I have written before.
public $$anonymous$$oveScript moveScript;
So since the script in question seems to be called "Cubemove", the line would have to be
public Cubemove moveScript;
I don't think you can drag components into an inspector field to assign the variable. so you would have to do it by code, like
moveScript = theRightGameObject.GetComponent<Cubemove>();
Your answer
