Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Friks · Jul 16, 2015 at 07:02 AM · stringreferencevalue

Grabbing a value using a string in the reference

The probable reason for me not being able to find the answer when searching is my lack of knowledge when it comes to coding terms and expressions.

 public Vital Strength;

Declaring a Vital called Strength. All good so far.

 Strength = new Vital(5, "Strength");

Here I am creating the Strength vital by calling upon the function Vital, passing the integer 5, and the string "Strenght" as arguments.

 public Vital(int _valueMod, string _statName)
 {
     currentValue = Player.current._statName.currentValue * _valueMod;
     maxValue = Player.current._statName.currentValue * _valueMod;
 }

Here is where it (obviously) does not work. My Player class has no declaration for _vitalName(because it wouldn't do anything, I can't grab the currentValue out from a string).

I want to grab a currentValue from a Stat, using not the direct reference to the Stat(as this would not make it useable for more than 1 stat as I would have to manually type the name of a Vital) but a reference that can be changed in the Call arguments.

Is this possible?

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 Hellium · Jul 16, 2015 at 07:23 AM

A "simple" way to do what you want is to use a System.Collections.Generic.Dictionary<string,Vital>. The, you could use the following syntax :

 Player.current[_vitalName].currentValue

See https://msdn.microsoft.com/en-us/library/xfhwa508.aspx

I think that the concept of reflexion could help you but I absolutely don't know how to use it.

Comment
Add comment · Show 13 · 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 Friks · Jul 16, 2015 at 03:21 PM 0
Share

Well I tinkered for some hours with your solution but couldn't come up with anything that worked.

$$anonymous$$y current efforts is making a list

 public List<Stat> Stats = new List<Stat>();


and inside the Stat constructor I have

 public Stat(string StatName)
 {
     _statName = StatName;
     currentValue = 1;
     Player.current.Stats.Add(this);
 }


and for the Vital constructor I have

 public Vital(int _value$$anonymous$$od, string vitalName)
 {
     foreach(Stat s in Stats)
     {
         if (s._statName == vitalName)
         {
             currentValue = s.currentValue * _value$$anonymous$$od;
         }
     }
 }


I get a nullreferenceException at "Player.current.Stats.Add(this);" however when trying to do this.

avatar image Hellium · Jul 16, 2015 at 03:34 PM 1
Share

I guess the error doesn't tell you where it occured ?

How many objects are listed in Stats ? Do you instantiate several Vitals ? If yes, how many ?

Try to do a simple for loop ins$$anonymous$$d of a foreach.

avatar image Friks · Jul 16, 2015 at 04:12 PM 0
Share

I'll link the entirety of the script for you to see: every class is [System.Serializable]

Player class:

 public class Player
 {
     public static Player current;

     public Vital Health;
     public Stat Strength;

     public List<Stat> Stats = new List<Stat>();

     public Player()
     {
         _name = "Name";

         Strength = new Stat("Strength");
         Health = new Vital(5, Strength._statName);
     }
 }

Stat class:

 public class Stat
 {
     public int currentValue;
     public string _statName;
     public Stat(string StatName)
     {
         _statName = StatName;
         currentValue = 1;
         Player.current.Stats.Add(this);
     }
 }



Vital class:

 public class Vital
 {
     public int currentValue;
     public int maxValue;

     public Vital(int _value$$anonymous$$od, string vitalName)
     {
         foreach(Stat s in Stats)
         {
             if (s._statName == vitalName)
             {
                 currentValue = s.currentValue * _value$$anonymous$$od;
             }
         }
     }
 }





Th nullreferanceexception error happens at lines:

     Strength = new Stat("Strength");

     Player.current.Stats.Add(this);


I'm kinda suspecting as I write this that I simply forgot to actually save the Player off somewhere for it to be referanceable?

avatar image Hellium · Jul 16, 2015 at 04:19 PM 1
Share

I think that's because your static Player is never instantiated. Thus, current doesn't exist, so does the list. The default constructor is not called.

It seems you are trying to implement a kind of Singleton but you have forgotten many things.

Add something like this in your player class :

 using System;
 
 public class Player
 {
    private static Player current;
 
    public static Player Current
    {
       get 
       {
          if (current== null)
          {
             current = new Player();
          }
          return current;
       }
    }
 }

Source : https://msdn.microsoft.com/en-us/library/ff650316.aspx

Then, you will be able to use your Player from anywhere.

avatar image Dave-Carlile · Jul 16, 2015 at 04:35 PM 1
Share

Where do you set the static current field? You need to set that in the Player constructor.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you rename a public variable and keep object references/values? 1 Answer

How Can I Change GetType() And GetField() And GetValue() ? 2 Answers

Make String Update for Value (GUI) 1 Answer

Is there a way to reference a script with a string? 1 Answer

Can you store a reference to a primitive value? 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