- Home /
How to use if statements for inspector in class?
I know this doesnt work but this is the only way i can explain it. How can i do something to this effect:
public Item myItem;
[System.Serializable]
public class Item
{
public string name;
if (name == "Weapon") // How Can I make it show variable damage if it is a weapon?
{
int damage = 10;
}
}
void Start ()
{
}
void Update ()
{
}
How can i use if statements to show variables in the inspector?
Answer by Hexer · Jul 25, 2015 at 10:49 PM
Set damage as a public variable. Also statements + logic are always called within the function/void brackets. Ponentional variables can be called from the class.
public int damage;
void Update() {
if(tag == "sword"){
int damage = 100;
}
if(tag == "dagger"){
int damage = 50;
}
}
The object this script is attached to has to have one of these tags. You can also do this in a switch/case statement. that should work a little bit cleaner.
This will not show the damage in the inspector. I think i have to use something with OnInspectorGUI?
This will show the damage in the inspector. Attach it to an object. Set the tag to "sword" and see the script in the inspector. Because it is a public variable it should be accesable in the inspector.
Your answer
Follow this Question
Related Questions
View script variables within an array of scripts in Inspector 1 Answer
Showing public class variables in another class in the Inspector. 1 Answer
to point a selfwritten class in the inspector (drag n drop) 0 Answers
Inspector not showing my public changes. 1 Answer
Automatically create enum based on class children types? (C#) 1 Answer