- Home /
switch scripts?
is this right to switch scripts?
var Script1 : GameObject;
var Script2 : GameObject;
private var startObject : int = 1;
function Start ()
{
Script1.enabled = true;
Script2.enabled = false;
startObject = 1;
}
function Update ()
{
if(Input.GetKeyDown("f") && (startObject == 1))
{
startObject = 2;
Script1.enabled = false;
Script2.enabled = true;
}
else if (Input.GetKeyDown("c") && (startObject == 2))
{
startObject = 1;
Script1.enabled = true;
Script2.enabled = false;
}
}
is this right to switch scripts?`
for any help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Though, it may be easier just to delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting. When you don't mark your code as code, the text gets html escaped and it gets really hard to reformat it.
I have formatted this one for you. Please check the code is the same as what you have.
can you answer the question? how do i enable disable the scripts?
I answered the question. Can you mark it as correct, or at the least leave a comment?
Answer by AlucardJay · Jan 08, 2013 at 11:23 AM
What you are doing is enabling/disabling the whole gameObject, not the script. For GameObjects, you should use SetActiveRecursively( true/false ), though this changed for Unity 4 (http://answers.unity3d.com/questions/359394/gameobject-setactiverecursively.html).
Typecast to the names of the scripts :
var Script1 : NameOfScriptOnObject1;
var Script2 : NameOfScriptOnObject2;
EDIT :
Create a new scene
Create an empty gameObject, call it HelloObject.
Create a script, call it HelloScript, paste this code :
#pragma strict
function OnGUI()
{
GUI.color = Color.green;
GUI.Box( Rect( (Screen.width*0.5) - 120, 40, 100, 35 ), "Hello ...." );
}
attach HelloScript to HelloObject
Create an empty gameObject, call it GoodbyeObject.
Create a script, call it GoodbyeScript, paste this code :
#pragma strict
function OnGUI()
{
GUI.color = Color.white;
GUI.Box( Rect( (Screen.width*0.5), 80, 100, 35 ), ".... Goodbye" );
}
attach GoodbyeScript to GoodbyeObject
Create an empty gameObject, call it SwapObject.
Create a script, call it SwapScript, paste this code :
#pragma strict
var theHelloScript : HelloScript;
var theGoodbyeScript : GoodbyeScript;
function Start()
{
theHelloScript.enabled = false;
theGoodbyeScript.enabled = false;
}
function Update()
{
if (Input.GetMouseButtonDown(0))
{
theHelloScript.enabled = true;
theGoodbyeScript.enabled = false;
}
else if (Input.GetMouseButtonDown(1))
{
theHelloScript.enabled = false;
theGoodbyeScript.enabled = true;
}
}
attach SwapScript to SwapObject
Now, click on SwapObject, and look in the Inspector. There are 2 variables :
var theHelloScript : HelloScript;
var theGoodbyeScript : GoodbyeScript;
Now, try and drag HelloObject onto theGoodbyeScript first, see how it won't drop in. Now put it where you think it should really go. Now drag in GoodbyeObject.
Now hit, play, then click the left-mouse-button, then the right-mouse-button.
Your answer
Follow this Question
Related Questions
Disable a script from another script 2 Answers
enable/disable specific components 3 Answers
How to Activate Script in another Object? 1 Answer
disable/enable script js 2 Answers
CS0119 Error for Script Disable? 1 Answer