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 asduffo · Aug 27, 2015 at 02:10 PM · c#getcomponentreferencevalue

Pass a value between script or else by reference?

Hello guys. I'm making a sort of editor for my game and i need to change multiple values from a script inside a component. Each value will get modified by pressing something like "increase+/decrease-" buttons and with this said, it means that every pair button will have a different script each

Explained simply:

The script with the values: let's suppose i have a MonoBehaviour script on a component like this:

 public class A : MonoBehaviour
 {
     public int a = 0;
     public int b = 0;
 }

Where a and b are some of the many values that i have to change costantly

and on the pair of buttons I have a script that inherits from another script like:

 public class C : MonoBehaviour
 {
     int reference;
 
     void Start()
     {
         
         SetValue();
     }

     public virtual void SetValue()
     {
         //Note: this is a way to get it. I don't really need GetComponent itself
         //the basic thing, however, would be that with a script i get a reference
         //on a value, then with another script inherited from this i override this
         //method and set another reference to another value like 'b'
         reference = GameObject.Find("a").GetComponent<A>().a;
     }

     public void ChangeValue()
     {
         //see below
     }
 }

Now let's suppose from script C on ChangeValue() i want to run a command like:

 ++reference; //or --reference

and when I do so, I will modify the value 'a' on the script A and not the variable 'reference' on C.

How could I do so without pointers?

NOTE: I can't, for my case, save the script instead and use A.a, of course, and any other kind of stuff like that (otherwise I would have to change on every script the code on ChangeValue() and it would be boring over than pointless and too long since there are maaaany values). I can only get the "reference" on the Start() method for my kind of need. Otherwise i wouldn't ask for how to do it.

Many thanks in advance and regards :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

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

Answer by _Gkxd · Aug 27, 2015 at 04:16 PM

I think the ref keyword is what you're looking for. This will only work for function calls.
https://msdn.microsoft.com/en-us/library/14akc2c7.aspx

You're script C might have a function that looks like this:

 void setValue(ref int someValue) {
      value = 123;
 }

To call this function, use setValue(ref A.a);

Though if you could do this, you could just set A.a directly.


If you have to make the fields references (i.e. the above won't work), the easiest way would be using a wrapper class:

 class PassingMyIntByReferenceUsingAWrapper {
     public int a;
 }

Here is a generic version of the above:

 class PassingMyTypeByReferenceUsingAWrapper<T> {
     public T value;
 }
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 asduffo · Aug 30, 2015 at 03:55 PM 0
Share

And exactly how should I use it? Something like

 Passing$$anonymous$$yTypeByReferenceUsingAWrapper reference;
     
     void Start()
     {
         reference.a = GameObject.Find("a").GetComponent<A>().a;
     }

Is that right? Correct me if I'm wrong but in this case shoudn't reference.a get affected and not the "GameObject.Find("a").GetComponent().a"'s value?

avatar image asduffo · Aug 30, 2015 at 04:20 PM 0
Share

NEVER$$anonymous$$IND! I realized by myself (accomplishment of the day) that I had to use it with the "A" class ins$$anonymous$$d than the int "a" value.

Now I can pretty much work out what I wanted, even if I'll have to modify a little more the code. One billion thanks :D

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

27 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

Related Questions

List of Structs variable values not changing 1 Answer

How to pull a variable from a separate C# script. 1 Answer

Editing other object's animator parameter... NullReferenceException? (VR) 0 Answers

WaitForSeconds in Coroutines do not work. 0 Answers

I am trying to change the value of a slider trough a script 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