- Home /
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?
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();
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)?
Why do you feel the need to refer to the weapons as strings? Why not just use
currentWeapon = SCAR;
or whatever?
$$anonymous$$y inventory is is string Array with a int index telling me what the currently selected item is.
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.
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'.
I've edited my question based on your input, but I'm still having issues - could you take a look?