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 IMoriarty · Dec 21, 2011 at 04:56 AM · variableclassdynamic

Dynamic reference of a Class variable

I've constructed several weapon data classes like so:

 public class Cleaver
     {
     public string WeaponName = "Cleaver";
     public string WeaponType = "Melee";
     public int WeaponRange = 0;
     public int WeaponDamage = 25;
     public float WeaponRefire = 2.0f;
     public int WeaponReload = 0;
     public float WeaponReloadTime = 0.0f;    
     }

I'd like to reference some of these variables in another class / script, which I'd normally do via dot notation (Cleaver.WeaponName).

However, if I wanted to reference .WeaponName because the weapon is not constant, how would I pass in a string variable to the call to dynamically get the appropriate Class's variable?

<----------Edit----------->

Base on Syclamoth's answer below I've edited my script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class WeaponItem
     {
     public string WeaponName = "Name";
     public string WeaponType = "Type";
     public int WeaponRange = 10;
     public int WeaponDamage = 10;
     public float WeaponRefire = 0.5f;
     public int WeaponReload = 0.5f;
     public float WeaponReloadTime = 0.1f;
     }
 
 public class WeaponManager : MonoBehaviour
 {
     private PlayerManager PlayerManager;
     public WeaponItem Cleaver;
     public WeaponItem SCAR;
     public WeaponItem Weapon3;
         
     Cleaver.WeaponName = "Cleaver";
     Cleaver.WeaponType = "Melee";
     Cleaver.WeaponRange = 0;
     Cleaver.WeaponDamage = 25;
     Cleaver.WeaponRefire = 2.0f;
     Cleaver.WeaponReload = 0;
     Cleaver.WeaponReloadTime = 0.0f;    
     
     SCAR.WeaponName = "SCAR";
     SCAR.WeaponType = "Ranged";
     SCAR.WeaponRange = 50;
     SCAR.WeaponDamage = 10;
     SCAR.WeaponRefire = 0.2f;
     SCAR.WeaponReload = 16;
     SCAR.WeaponReloadTime = 4.0f;    
     
     Weapon3.WeaponName = "Weapon3";
     Weapon3.WeaponType = "Ranged";
     Weapon3.WeaponRange = 50;
     Weapon3.WeaponDamage = 10;
     Weapon3.WeaponRefire = 0.2f;
     Weapon3.WeaponReload = 16;
     Weapon3.WeaponReloadTime = 4.0f;
     
     void Start()
     {
         PlayerManager = gameObject.GetComponentInChildren<PlayerManager> ();    
     }
 }

I'm getting a compile error when I try to define the Cleaver's properties:

"error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration"

Which I don't really understand - is it complaining that Cleaver doesn't exist yet, so it's assuming I'm attempting to create it?

Comment
Add comment · Show 9
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 syclamoth · Dec 21, 2011 at 07:17 AM 0
Share

Yeah, you need to set up all of the

 /name/.whatever

stuff inside of a function! You can't just set it up like that, without using special constructors. The most effective way to set this stuff up using Unity would be to do this-

Add the line

 [System.Serializable]

just before the line

 public class WeaponItem

Then, your public weapons should show up in the inspector! Set up all their values there, so you don't have to hardcode them.

EDIT Just to be clear, I'm telling you to delete all the lines between 'public WeaponItem Weapon3' and 'void Start()'

Another thing, to avoid further confusion between classes and instances, you should follow the rule-

Capitalise Classes

camelCase instances

So,

 public CapitalisedClass instance = new CapitalisedClass();
avatar image IMoriarty · Dec 21, 2011 at 07:38 AM 0
Share

Okay, so I've done that, which was a much better way to set those up, but I remain with my initial question - how do I make currentWeapon = (stringvar)?

avatar image syclamoth · Dec 21, 2011 at 07:41 AM 0
Share

Why do you feel the need to refer to the weapons as strings? Why not just use

 currentWeapon = SCAR;

or whatever?

avatar image IMoriarty · Dec 21, 2011 at 07:44 AM 0
Share

$$anonymous$$y inventory is is string Array with a int index telling me what the currently selected item is.

avatar image syclamoth · Dec 21, 2011 at 07:46 AM 0
Share

Why not, ins$$anonymous$$d, make it a Weapon$$anonymous$$anager array? Wouldn't that be simpler? Or do you have several types of inventory items?

If so, make 'Weapon' inherit from 'InventoryItem' and do it like that.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by syclamoth · Dec 21, 2011 at 06:26 AM

It looks like you are mixing up static classes and instances.

If you have a class 'Weapon'-

 public class Weapon
 {
     public string WeaponName = "Cleaver";
     public string WeaponType = "Melee";
     public int WeaponRange = 0;
     public int WeaponDamage = 25;
     public float WeaponRefire = 2.0f;
     public int WeaponReload = 0;
     public float WeaponReloadTime = 0.0f;  
 }

Then you can then use

 public Weapon cleaver;
 public Weapon woodenSpoon;
 public Weapon bowl;

Then, use

 cleaver.WeaponName = "Cleaver";
 woodenSpoon.WeaponName = "Wooden Spoon";

And so on. Importantly, the fact that the different weapons have different names doesn't change the name of the variable in the class. So, if you have

 Weapon currentWeapon = cleaver;

 // Prints "Cleaver"
 Debug.Log(currentWeapon.WeaponName);

 currentWeapon = woodenSpoon;
 
 // This time prints "Wooden Spoon", despite being the exact same line!
 Debug.Log(currentWeapon.WeaponName);

You get a different result depending on which weapon is the 'currentWeapon'.

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 IMoriarty · Dec 21, 2011 at 07:04 AM 0
Share

I've edited my question based on your input, but I'm still having issues - could you take a look?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Variable Resolution 2 Answers

How to access variable from other class? 1 Answer

Static Class unmodifiable variable 1 Answer

Difference between Rigidbody2D and rigidbody2D 2 Answers

How may i get a variable from another script onto my wanted 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