Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by roeming · Jan 11, 2017 at 04:35 AM · objectvariablevariablestransfer

Change a variable in a different object?

im making a monster card game, how would i be able to set a hand of cards be able to place cards? should i use update and check a variable for who's turn it is?

but in a broader sense how would i tell one object to tell another object that int i = 1 instead of 0 i could think of doing this by spawning an object that holds this information, colliding it into the second object, have the second object read the information from the object, then delete the object, and update its own fields, but this seems tedious and rube goldberg-y.

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
3

Answer by Scoutas · Jan 11, 2017 at 05:31 AM

For example. you have two scripts and you have one attached to an object with a tag "ObjectOne" and another one with a tag "ObjectTwo".

  class ScriptOne: MonoBehaviour {
       // This is attached to ObjectOne
       // This would be the integer you would change from ObjectTwo
       public int integerToChange = 0;

       // And for the sake of it, this is the function you will call from ObjectTwo
       public void MethodToCall () { 
            Debug.Log(integerToChange);
       }
  }


  class ScriptTwo: MonoBehaviour {
       // What you need to do, is to get a reference, of type GameObject so you could store the 
       // ObjectOne and ScriptOne so you could store 'ScriptOne' component. 
       // It's a component, when it's attached to a GameObject
       GameObject referenceObject;
       ScriptOne referenceScript;
  
       void Start(){
       // Now in the start method, you need to get the references of these things inside a scene.
       // To find the GameObject with tag "ObjectOne" you would use:

       referenceObject = GameObject.FindObjectWithTag("ObjectOne");

       // this method searches the scene for object, which is tagged "ObjectOne" and
       // assigns it to the 'referenceObject' variable
       // Now you need to get the component called 'ScriptOne' that's attached to the object.

       referenceScript = referenceObject.GetComponent<ScriptOne>();

       // you call GetComponent <ComponentType>() on the referenceObject and it returns
       // the component of the type that you specified between < > (if it has one).

       // Now you can change the variable inside the ObjectOne, from the inside of ObjectTwo

       referenceScript.integerToChange = 1;

       // You can launch a method that's defined in 'ScriptOne' as well

       referenceScript.MethodToCall();

       // Note: both the integerToChange and MethodToCall() has to have 'public' in front of them
       // so that you would be able to call them from outside the ScriptOne.
       
       }
  }

I really hope it's clear enough.

Comment
Add comment · Show 2 · 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 Dimitris4 · Mar 16, 2019 at 11:20 AM 0
Share

There is no FindObjectWithTag , Did I do smth wrong?

avatar image Hellium Dimitris4 · Mar 16, 2019 at 04:12 PM 0
Share

https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html

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

94 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use enum hidden value as something other than int? (C#) 2 Answers

Choose something randomly based on its grouping 0 Answers

Problem with the Inspector and Script 1 Answer

scripting problems please help 0 Answers

Calling variables from other script 0 Answers


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