- Home /
Duplicate Question
How to activate a script from another object?
What I'm trying to do here, is when you click a button, it activates a script that makes an object change the sprite image. There is 4 images that can be changed to, and each of them has 25% to be chosen. How do you activate it by clicking another object and change image randomly? I want this to be continuous so each time you click the object it makes the other object change image. Anyone can give me a sample code? I'm pretty new to unity. Any help would be appreciated. Thanks :D
Answer by DESTRUKTORR · Jul 08, 2014 at 12:11 PM
What you're asking for here is several things that you should really be trying on your own. Unity Answers does not operate like a restaurant. You don't get to just place an order and expect that someone will complete it for you. So, that being said, I will not write a script that does all that for you.
What I will do is explain how you can figure that all out for yourself.
Here's what you should do:
Make a new script file.
In said script file, add the OnGUI() function
In the OnGUI function, draw your button (with either GUI.Button or GUILayout.Button) - putting it as the condition of an if statement
Inside that if statement, make a call to the method/function in the object you wanted to call
That last step would seem to be the one that's throwing you off the most. However, that step can be achieved through a plethora of different means, all of which are more or less appropriate for different situations.
I'll just cover two basic ones, here, though you should note that these are not exactly computationally efficient, and should only be executed as absolutely necessary. If it is absolutely necessary to call them every frame, I would suggest you find another way to organize your scripts' interactions.
If the scripts and GameObjects you need to reference exist in the scene file (i.e. outside play mode), then you should just add some public fields with matching types with the types of scripts you need to reference, and make as many of them as you need at runtime. These will show up in the Inspector window, in the editor, when inspecting the script.
If you do not know how many there will be, when in-game, or you don't have any references to them at all, before the game starts, then you can use GameObject.Find, if you know the name of the GameObject, OR you can use FindObjectWithTag or FindGameObjectsWithTag, if you've set the tag variables on the GameObjects.
Alternatively, if these objects are continuously created, you COULD just have them find the object of the script that references them (either in this way, or in other, similar ways) and then send that object a reference to them.
If you are unsure how any of this might work, I would strongly suggest you focus more on learning more about programming in JS or C#, as these are relatively simple concepts, in programming, and it is rather important that you understand them, should you wish to continue.
Also, look over the Unity Manual. It's filled with tutorials on basic stuff like this.
Don't take my attitude about the "write my script for me" thing personally. I'm not trying to call you out. The reason we expect that you've put forth a bit more effort is mainly because this is supposed to be a place for learning, and one does not learn by having others do their work for them. So, in reality, it's for both your benefit, and the benefit of others that come after you, and find this answer.