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 d112570 · Jun 16, 2015 at 09:33 AM · setters

Using Buttons to set Setters not working?

I made a new project for this demo. I created 2 Buttons and 2 c# scripts. When I set them inside the method it works, but when I access them outside the method, the value gets reset to 1. Here is my code.

 using UnityEngine;
 using System.Collections;
 
 public class xtest : MonoBehaviour{
 
     int points1 = 1;
     int points2 = 1;
 
     public int Points1 {
         get{ return points1; }
         set{ points1 = value; }
     }
 
     public int Points2 {
         get{ return points2; }
         set{ points2 = value; }
     }
 }

//Next script is here//

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class xtestButton : MonoBehaviour
 {
     xtest button;
     Text t;
 
     void Start ()
     {
         button = gameObject.AddComponent<xtest> ();
         t = GameObject.Find ("test").GetComponent<Text> ();
     }
     
     void Update ()
     {
         print(button.Points1.ToString() + " - " + button.Points2.ToString());
     }
 
     public void Button1 (int button1)
     {
         button.Points1 = button1;
         t.text = button.Points1.ToString() + " - " + button.Points2.ToString();
     }
 
     public void Button2 (int button2)
     {
         button.Points2 = button2;
         t.text = button.Points1.ToString() + " - " + button.Points2.ToString();
     }
 }


Results; I press button named 10, I get "10 - 1" so far so good. I press button named 20, I get "1 - 20" not so good. It should read 10-20. I press button named 10, I get "10 - 1" also not correct, should read 10-20 since both buttons now were set.

In the update function I get simultaneous (1-1 and 10-1), or (20-1 and 1-1) depending on which button pressed. What is going on here? I told it to print 2 values I get 4 printed? It prints the setted values and the original values at the same time. huh?

Here is a pict of my button containing the script. There are no other codes or anything else involved. It's a clean project. alt text

1.png (63.7 kB)
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 Bunny83 · Jun 16, 2015 at 10:35 AM

You should have mentioned your other question as it seems to be the same problem but this time you finally provided enough information.

Your problem is that it seems you have lost track of where which instances of what classes are actually used.

To me your setup looks like this:

  • You have two gameobjects "Button1" and "Button2".

  • Each of those buttons has an UI.Button script and a xtestButton script attached

The problem here is that the xtestButton script adds an instance of the "xtest" script to the gameobject it's attached to. Since you have two xtestButton instances, each gameobject (Button1 and Button2) will have it's own "xtest" instance as well.

Since you setup the UI.Button script of "Button1" to invoke the "Button1()" method of it's xtestButton script instance and the Button script on "Button2" to invoke the "Button2()" method of the second xtestButton instance on the Button2 gameobject, each button works on completely seperate instances.

Two solutions:

  1. Remove the "xtestbutton" instances from both buttons. Add your "xtestbutton" script to a seperate gameobject and drag that gameobject in the "object reference field" of your onclick handler on each button. That field is below the "Runtime Only" dropdown field. Now you can select your desired method you want to run. If you do that on both buttons, both will actually work with the same instance of your xtestButton script.

  2. Keep the xtestButton on each button, but remove the AddComponent call from Start. Instead add the "xtest" component manually to a seperate gameobject. Change the "button" variable in your xtestbutton script from private to public. Now you can simply drag the gameobject with your xtest script onto that variable on each button. In this solution each button still has it's own xtestbutton script instance, but both work on the same "xtest" instance.

ps: class names should use UpperCamelCase.

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 d112570 · Jun 16, 2015 at 10:51 AM 0
Share

Hey thanks man, that helped a lot. Changing scenes, the gameObject would no longer be available, does that mean I will lose all my setters value? Or is there a way I can transfer the same gameObject to the next scene, so I can keep the values? Like moving from the main menu scene to the game level 1 scene.

avatar image Bunny83 · Jun 17, 2015 at 12:55 AM 0
Share

@d112570:
Sure, that's what DontDestroyOnLoad is good for. An object that has been "marked" with that method will not get destroyed when a new scene loads. However keep in $$anonymous$$d that if that object is in say "Scene1" and you somehow return to that scene you will end up with two instances. That's because when you load the scene again it again contains that gameobject. So each time you load that scene again you get another instance.

Also note that DontDestroyOnLoad only workd on top-level gameobjects. If you mark a child object with that method but not it's parent, the object will get deleted at a scene change since the parent is destroyed and with it all childs.

The best approach is to have an additional loading scene which you only load once when the application starts and you never (ever) load the scene again.

Of course links to that object has to be established manually via code (FindObjectOfType / GameObject.Find + GetComponent / Singleton).

avatar image d112570 · Jun 18, 2015 at 07:43 AM 0
Share

Another problem is when I create a new script and get the setters my results are are defaults of 0. Script a is my getter and setter script, script b is to set my getters, script c is to get my setters.

I can call my getters and return my values in script b, when I use script c I get its default values. Script b and c is on my parent game object. Anyway to resolve.

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

22 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

Related Questions

Accessing class properties through getters & setters in editor mode 1 Answer

Javascript : Get variable & run a method from another script 0 Answers

getter and setter not working 1 Answer

Is it possible to change the "jump" positive button during runtime? 1 Answer

Proper use of c# properties? 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