Disable script using another script
Yeah, how do I do that?
There are multiple ways. Once you have a reference to the script you can call otherScript.enabled = false; to disable the update loop in the script. Please specify an example on how/when this should happen, if you want a better answer.
Hello, if my answer was correct please help me as well,and other people having the same issue by selecting my answer as correct. Thank you!
Sorry, I've been busy and haven't managed to respond. It has worked well in $$anonymous$$onoDevelop, however in Unity, I'm still getting "scriptname" is an unknown identifier, even though I followed that link you posted.
Have you tried putting the C# code in a folder called "Standard Assets"? $$anonymous$$ake a folder in Assets called Standard Assets. So thats Assets/StandardAssets. Put the C# code in there then try using the code below and let me know what happens.
I have moved the C# script into Standard Assets and kept the JS script in Assets. Still doing the thing.
Answer by Political Peace Party Studios · Aug 25, 2015 at 12:47 AM
Here is the code you are looking for.
//Disable another script as soon as your press Play
function Start () {
gameObject.Find("gameobject").GetComponent (scriptname).enabled = false;
}
"gameobject" = the name of the game object that the script is on in the hierarchy.
"scriptname" = the name of the script that is on the game object.
(TIP) - To turn the script back on just change the word (false) to (true).
I've put in the line and I'm getting ""enabled" is not a component of UnityEngine.Component".
I just tested this code and it worked. You are using javascript right?
I am using JavaScript, yes. However the code I'm trying to disable is in C#, in case that made any difference.
Please show us the code you used and the script so we can pinpoint where the error is occuring...
Yes Javascript can't access the C# code that is the problem. I believe the fix would be putting the C# code in a folder called Plugins or standard assets Assets/plugins/C#code then it might work. Check this out http://answers.unity3d.com/questions/252865/accessing-a-c-field-from-javascript.html
Answer by unpluggeDloop · Jan 31, 2020 at 01:05 AM
void Start () {
this.GetComponent<YourScriptName>().enabled = false;
}
...or check yt video https://www.youtube.com/watch?v=kgOttwh8-fE
Answer by Whynote · Mar 10 at 09:56 AM
if (GameObject.Find("yourobjectname") != null) // just to avoid null pointer exceptions
{
GameObject.Find("yourobjectname").GetComponent<yourscriptname>().enabled = false;
}
yourobjectname = Name of your object that has the script in it
yourscriptname = the script you want to disable (or enable by replacing "false" with "true"
Hope this helps it worked fine for me @VLunarFangV
Your answer
Follow this Question
Related Questions
How read data from simulink to javascript? 0 Answers
How do you disable all controls? 0 Answers