I’m just trying to enable/activate a script from another object. It’s as simple as that! But I just can’t get it working.
I asked a few questions on unity answers but none of them helped and people didn’t reply to my comments. I have a GUI Texture/Button and it should be activating the other script.I have got no errors in the console, and I couldn’t figure out what I have done wrong here. Heres my code(for the button and by the way I’m using javascript) :
function OnMouseDown () {
Debug.Log("Clicked!");
GameObject.Find("theobject").GetComponent(thescript).enabled = true;
}
Does anyone have any ideas? I really need help. Any help is much appreciated. Thanks in advance!
Yes, all of them above. But i have 1 question. I have the OnEnable function on the other script so it won’t play by it self when playing the game. Does that cause the problem?
Graham has already asked, but are you sure GameObject.Find(“the object”) returns the object you want? Or does it return null? If I am understanding you correctly, you want another script to activate an inactive object? GameObject.Find only returns active objects.
Yes. But you ignored me the first time, so I expect you’ll just ignore me this time.
Does GameObject.Find(“object”) actually find a game object? Do you have a game object called “object”? Maybe this part of your script fails. (Which means you’ll get a null reference exception.)
Does GetComponent(script) work? In your original post you used thescript and now it’s script. What happens if you use double quotes like GetComponent(“script”)? The argument to GetComponent should be the type, if it’s known. Since I don’t know what your script is called, it’s hard to know what you should use.
Oh my god, that simple short statement was the best answer I’ve ever had in a week! Thankyou so much!! All I had to do is disable the script from the object! Can I ask a question. If I press the button, it would just change the sprite non stop. Do you have any idea how to play the script only once every time I press the button?
Sounds like you need to have a bool to track when pressed and set it to true, then catch the release event and set it to false. In the pressed event you would immediately return when the flag is true.
Theres no errors(except in javascript bool is boolean but I fixed it) but it didn’t really work. It still stayed the same like flashing through sprites.
Edit: I changed it to this. I think it helped a bit but…
function OnMouseDown () {
GameObject.Find().GetComponent().enabled = true;
}
function OnMouseUp () {
GameObject.Find().GetComponent().enabled = false;
}