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 /
avatar image
1
Question by wildarch · Nov 21, 2013 at 04:59 PM · dynamicstatic variablelinking

How to dynamically access scripts from other gameobjects?

So here's what I plan on building in Unity: I will be creating a 2D simulation of several logic gates, LEDs, buttons and such. All these components will be prefabs, so the user can choose which and how many to load. The components can be linked by connecting them using "wires" that transmit the current from one component to the other.

My idea was to add a script with a static variable "current" to every gameobject and have other gameobjects read the variable from that script in order to retrieve the current emitted from the connected gameobject. The real problem is that I can't get Unity to dynamically assign a script to a variable. So my question is: Is this possible, or might there be a better solution?

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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hamstar · Nov 21, 2013 at 05:28 PM

I'm not sure you want to use a static variable. If a variable is static, all game objects which use the class/script "share" the value of the static variable. Wouldn't you want each game object to have it's own value for current?

To access a variable in a script attached to another game object, you first need a reference to that GameObject. One way would be to do this on collision, another is using `GameObject.FindWithTag`. How you do it depends on your design.

You then need a reference to the component (script) which has the variable you want. Do this with `GetComponent`.

You can then access any public variables and methods in that component using dot notation.

Here's a C# example which checks another electric object's current on a collision. Assume objects with the tag "Electric Object" have an the ElectricCharge script attached.

 public class ElectricCharge : MonoBehaviour {
     public int current = 0;
 
     void OnCollisionEnter(Collision collision) {
         if (collision.gameObject.tag == "Electric Object") {
             int otherObjectCurrent = collision.gameObject.GetComponent<ElectricCharge>().current;
             Debug.Log("The other object's current is: " + otherObjectCurrent);
         }
     }
 }


Comment
Add comment · Show 3 · 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
avatar image hamstar · Nov 21, 2013 at 05:31 PM 0
Share

I just re-read your question and I'm not sure if this is what you were asking. I can delete this if it's not helpful.

avatar image wildarch · Nov 21, 2013 at 06:21 PM 0
Share

$$anonymous$$y approach was exactly the same. The only reason my script threw an error turned out to be a syntax error. The project now finally compiles and it seems to run just fine. Thank you so much for helping me out! Problem solved.

avatar image hamstar · Nov 21, 2013 at 08:20 PM 0
Share

Ok, great!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Script loads font, how do I change the color? 2 Answers

What's the best way to store a reference to a script asset? 2 Answers

How to parent a GUI text to current game object 3 Answers

Which is better: script or animation (which needs to change with scripting)? 1 Answer

Is it possible to compile and run C# code on-the-fly on mobile? 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