- Home /
Turn off a script and then back on?
I need to turn off a script in my game and then turn it back on. Pretty much the point is here: I got ToggleCursor.cs I need to turn off MouseLook.cs Someone help me! :D
"Did not work" means what? Did it generate an error? If you look in the inspector at the '$$anonymous$$ouseLook' component on the 'PlayerCamera', does the script get disabled? Do you have code that is reenabling this script that is perhaps getting executed? Any chance there are two $$anonymous$$ouseLook scripts on the camera? Etc. You need to dig deeper, because finding the component on the right object and setting the enabled flag will turn off the component.
Answer by robertbu · Sep 18, 2014 at 05:29 PM
You need to find the game object the script is on, either by tag, or by name, or by someone mechanism. Assuming it is on the camera:
Camera.main.gameObject.GetComponent<MouseLook>().enabled = false;
So what is the error? We cannot see your code, so we have to depend on you giving us enough information to figure out what is going on. The concept above is correct. You need to get the component from the specific game object that has the $$anonymous$$ouseLook script and set the enable flag. Assu$$anonymous$$g your camera has a unique name, you can try the alternate:
GameObject.Find("NameOfCamera").GetComponent<$$anonymous$$ouseLook>().enabled = false;
Thank you robertu soo much you saved me a loats of time just write a real comment not a reply so i can set it as solution! :D
Answer by SnotE101 · Sep 18, 2014 at 05:26 PM
Create a new script and paste this in there.
bool disableScript; public scriptYouWantToStop scriptYouWantToStop;
bool disableScript; public scriptYouWantToStop scriptYouWantToStop;
void Update
{
if(disableScript)
{
startCourtine(pauseScript());
}
}
ienumberable pauseScript();
{
scriptYouWantToStop.enabled = false;
yield return new waitforsecound(5);
}
This is untested and probably has some spelling errors. Just drag the script you want to pause into the variable scriptYouWantToStop in the inspector. (It is easier if this script is on the same GameObject).
GoodLuck!
Yeah thanks but how do i start it up again, i need like a toggle type of a thing, the point here in the script is like you press e to toggle cursor/is it showing or not and then when i am showing the cursor i want to disable the script that allows looking around, and then re-enable it once i press/toggle e again.
$$anonymous$$ake sure to fix the errors before using this answer.
Answer by zeeshandar · Sep 18, 2014 at 05:39 PM
public GameObject scripttodisable = GameObject.Find("LevelNo").GetComponent<Yourscriptname>();
scripttodisable.setactive(false);`
Or GameObject.Find("LevelNo").GetComponent().enabled=false;
LevelNo is the object which contains the script
Typos, typos, typos...
And make sure to put your code in code view or any less than greater than wont show.
I did the code and I got this: Assets/ToggleCursor.cs(6,75): error CS0029: Cannot implicitly convert type $$anonymous$$ouseLook' to
UnityEngine.GameObject'