Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 08, 2014 at 12:43 PM by meat5000 for the following reason:

Duplicate Question

avatar image
0
Question by markzareal · Jul 08, 2014 at 11:52 AM · c#javascriptrandomization

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
2
Best Answer

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:

  1. Make a new script file.

  2. In said script file, add the OnGUI() function

  3. In the OnGUI function, draw your button (with either GUI.Button or GUILayout.Button) - putting it as the condition of an if statement

  4. 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to call a c-sharp function from javascript? 1 Answer

How to implement a low pass filter for accelerometer? 2 Answers

How do I create an database to store Question and answers 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges