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
0
Question by d112570 · Jun 15, 2015 at 07:38 PM · getset

Getter Working, Setter not.

 using UnityEngine;
 using System.Collections;
 
 public class BaseSkills : MonoBehaviour
 {
 int strBase = 1;
 
 public int Strength {
         get{ return strBase; }
         set{ strBase = value; }
     }

Top is my getter and setter code. Where Strength base value is 1.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class AttributesSetup : MonoBehaviour
 {
 public int strMin;
 public int myClass;
 BaseSkills class1;

     public void StrMin (int strMin) {
         class1.Strength = 5;
         print(class1.Strength); // I get 5

     }
 // Here I set the Strength value to 5.

     public void Strength (int strengthLevel)
     {
         print(class1.Strength); // I get 1
     }

Here I retrieve the value and get 1 and not 5, if I print after I set the value I get value 5.

I need the value set, so I can use the set value everywhere in any script. This is my first day in using getters and setters. I omitted some codes so its easier reading.

Comment
Add comment · Show 1
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 15, 2015 at 10:03 PM 0
Share

As much as I learned it, when I set class1.strength = str$$anonymous$$in, it should be set to 5 depending on character class value. But when I call it for later use, it went back to default. The $$anonymous$$ethod is attached to an new game button via GUI, and when you press the button, 5 gets called to str$$anonymous$$in, and different values to all other attributes. When pressing the arrow key, it should add Base Strength + Points added + Buffs. In this case just Strength + points.

3 Replies

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

Answer by d112570 · Jun 16, 2015 at 11:13 AM

The problem was solved at this post.

http://answers.unity3d.com/questions/987583/using-buttons-to-set-setters-not-working.html

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 DiegoSLTS · Jun 15, 2015 at 07:49 PM

Your StrMin method receives an strMin integer value that's never used, then changes the Strength property of class1 to 5 and prints it.

Your Strength method (in AttributesSetup) receives one strengthLevel value that's never used, and the prints class1.Strength value.

The missing info here is the code that calls StrMin and/or Strength, if you call Strength before StrMin, nothing else (at least in the code you showed) will change it's default value of 1.

Also, you are saying that the setter is not working but as you see when calling StrMin, the setter DOES work, if it wasn't working you wouldn't be getting a "5" printed.

Check again that you call Strength (the method of AttributesSetup) after you've set the Strength (the property of class1) to a different value. If it's still not working share some more code to find the real problem.

Comment
Add comment · Show 4 · 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 01:46 AM 0
Share

I did a little test, if I add the code inside the void awake it works.

 void Awake ()
 {
 class1.Strength = 5;
 }
 
 public void Strength (int strengthLevel)
      {
          print(class1.Strength); // I get 5
      }

if I move "class1.Strength = 5;" back inside the method it does not work.

 public void Str$$anonymous$$in (int str$$anonymous$$in) {
          class1.Strength = str$$anonymous$$in; // str$$anonymous$$in = 5
          print(class1.Strength); // I get 5
   }
  public void Strength (int strengthLevel)
      {
          print(class1.Strength); // I get 1
      }
avatar image DiegoSLTS · Jun 16, 2015 at 02:02 AM 0
Share

Awake is called automatically by Unity when the scene is created. "Str$$anonymous$$in" is a method you have to call by yourself somewhere, just like "Strength".

Just having the methods in some script won't call them, it doesn't matter if you have Str$$anonymous$$in implemented above Strength, you have to call them.

Again, show us the code where you CALL Str$$anonymous$$in and Strenght, not the code where you implement those methods. $$anonymous$$ake sure Str$$anonymous$$in is called at least once before the call to Strength (if your code is hard to follow you can check that by adding prints at the begining of each method).

avatar image d112570 · Jun 16, 2015 at 02:11 AM 0
Share

I added the full code below. When I press new game then str$$anonymous$$in = 5. And at the new game window I can edit the strengthLevel using the gui-buttons.

avatar image d112570 · Jun 16, 2015 at 02:18 AM 0
Share

Here ist a screenshot of the button. AttributesSetup is also attached to the button. alt text

1.jpg (229.6 kB)
avatar image
0

Answer by Jessespike · Jun 15, 2015 at 07:51 PM

The get/set property looks OK. So it must be something else, from the code you're showing, it looks like the Strength function simply prints out class1's Strength, which by default would be 1.

  public void Strength (int strengthLevel)
  {
      class1.Strength = strengthLevel;  //add this to set the strength
      print(class1.Strength);
  }

need more codes, what's the order that you're calling these? Not enough info to be certain.

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 d112570 · Jun 15, 2015 at 09:52 PM 0
Share

The full code is here. str$$anonymous$$in = 5

 public void aStr$$anonymous$$in (int str$$anonymous$$in) {
         str.text = str$$anonymous$$in.ToString();
         GameObject.Find ("Strength").GetComponent<AttributesSetup> ().str$$anonymous$$in = str$$anonymous$$in;
         class1.Strength = str$$anonymous$$in;
 print(class1.Strength); // I get 5
     }
 
 public void Strength (int strengthLevel)
     {
         var pointsLeft = int.Parse (points.text);
         if ((count+str$$anonymous$$in) >= str$$anonymous$$in && strengthLevel == 1 && pointsLeft >= 1) { // Add
             count = count + strengthLevel;
             pointsLeft--;
         } else if ((count+str$$anonymous$$in) != str$$anonymous$$in && strengthLevel == -1) { // Subtract
             count = count + strengthLevel;
             pointsLeft++;
         }
         str.text = (count+str$$anonymous$$in).ToString();
         points.text =  pointsLeft.ToString();
         class1.StrengthPoints = count;
         var Strength = class1.StrengthPoints + class1.Strength;
         print("Strength " + Strength + " - Points given " + class1.StrengthPoints);
         print(class1.Strength); // I get 1
     }

When I start new game, the void str$$anonymous$$in gets called. When adding extra points to strength void strength level gets called.

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

21 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

Related Questions

How can I get Rigidbody.velocity instead of set it? 1 Answer

C# public parameter with custom get/set doesnt shown in the Inspector 4 Answers

Joystick DPad axis to bool 1 Answer

Help with static variables not being saved 1 Answer

boolean questions get, set, in java. 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