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 /
avatar image
0
Question by II_Spoon_II · Sep 20, 2018 at 09:23 AM · scripting problemscript.

Not changing value after being set.

Hello,

So, my problem is this: I am accessing a variable from another script, the player can change that variable by input, the problem is for example, I have a class A and anothee one called B, A contains a variable X, B accesse's X from A using, I want B to keep the initial X variable even if it changes in A, is it possible?

 public class A : MonoBehaviour {
     
     public GameObject X;

     OnMouseDown() {
     X = gameObject;
    }

     
     }
 

 

 public class B : MonoBehaviour {
 GameObject x;
 
 void update
 {
  GameObject A0 = GameObject.Find("A's GameObject");
         A a = A0.GetComponent<A>();
 x=a.X
 //Do something with x
         }
 }







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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KittenSnipes · Sep 20, 2018 at 10:42 AM

@II_Spoon_II So basically a simple script like that should work. If x is not set already then it will be set otherwise it will remain the same and because you set x in the other script before setting in in the original script then you will surely have the value you need.

 public class A : MonoBehaviour {
      
     public GameObject X;
     private B other;
     
     void Start() {
         other = GetComponent<B>();
     }
 
     void OnMouseDown()
     {
         other.UpdateX(gameObject);
         X = gameObject;
     }
      
 }
  
  
  public class B : MonoBehaviour {
       public GameObject x;
       private bool xRecieved = false;
       public void UpdateX(GameObject obj)
       {
            if (xRecieved == false) {
                x = obj;
                xRecieved = true;
            }
       }
  }
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
avatar image
0

Answer by Legend_Bacon · Sep 20, 2018 at 10:05 AM

Hello there,


The way you do it here will always return the CURRENT value of X, not the initial one.

What you want to do instead is have a variable on B (let's call it "initialX"), that gets assigned to X on Start().

Then, as long as you don't change it, that initialX will always be the value that X had at the beginning.


private GameObject initialX = null;

 private void Start()
 {
    GameObject Aobject = GameObject.Find("A's GameObject");
    initialX = Aobject.GetComponent<A>().X;
 }



Hope that helps!

Cheers,

~LegendBacon

Comment
Add comment · Show 1 · 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 II_Spoon_II · Sep 20, 2018 at 10:15 AM 0
Share

The problem is initially X is null, the player selects while in game so I don't think Start will be ideal

avatar image
0

Answer by Hellium · Sep 20, 2018 at 10:58 AM

Since a GameObject is a reference type, the only solution is to have two fields.

 public class A : MonoBehaviour
 {
      private GameObject x ;
      public GameObject InitialX
      {
           get ; private set ;
       }
 
      void OnMouseDown()
      {
          x = gameObject;
          if( InitialX == null ) InitialX = gameObject ;
      }      
  }
 
  public class B : MonoBehaviour
 {
     GameObject x;
  
      void Update()
      {
           // The following is **not advised at all**
           // Calling Find and GetComponent every frame is resources-consuming
           GameObject A0 = GameObject.Find("A's GameObject");
           A a = A0.GetComponent<A>();
           x = a.InitialX ;
           //Do something with x
       }
  }
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

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

166 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 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

help with instantiating gameobject at random postition from a target gameobject? 1 Answer

Need a bit of help implementing something into a code 1 Answer

How can i change a gameobject scale to only one direction ? 0 Answers

How can i create a Plane using this Mesh generator script ? 0 Answers

How to change button text in simple character shop ? 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