- Home /
Changing class variables
Is there a way to do this:
EDIT: I changed the code below
enum ItemType {
Resource = 0,
Weapon = 1,
Medical = 2,
Building = 3
}
enum MedicalType {
Heal = 0,
Feed = 1,
Drink = 2,
Multiple = 3
}
class item {
var Name : String;
var Type : ItemType = ItemType.Resource;
var texture : Texture2D;
if (Type == ItemType.Building) {
var Building : GameObject;
}
if (Type == ItemType.Medical) {
var MedType : MedicalType = MedicalType.Heal;
var MedicalAmount : int;
}
if (Type == ItemType.Weapon) {
var Weapon : GameObject;
var AmmoType : String;
var Range : float;
}
}
Could you describe what you are after because you seem to have it already. Just place the if in a Start and done with it.
using UnityEngine;
using System.Collections;
public class Item : $$anonymous$$onoBehaviour
{
public string name = "";
}
public class Weapon : Item
{
public int count = 0;
}
public class Resource : Item
{
public Texture2D texture;
}
public class $$anonymous$$ainClass : $$anonymous$$onoBehaviour {
public GameObject[] gameObjects = new GameObject[4];
// Use this for initialization
void Start()
{
//Replace Cube_P0 with existing GO etc
gameObjects[0] = GameObject.Find("Cube_P0");
gameObjects[0].AddComponent<Weapon>();
}
Item item = new Weapon();
}
By inheriting from $$anonymous$$onBehaviour, you can add the class as a component.
Alternatively, add Item as a variable e.g.
Item item; item = new Weapon(); item = new Resource();
Answer by richyrich · Oct 23, 2014 at 12:49 PM
Yes, but the code would be meaningless given both integers within the enum are currently set to the same value. Also, you can't declare a variable within the if-statement and then not use it.
What do you mean I can't declare a variable in the if-statement and then not use it? I want there to be some options specified by which type the Type is.
If you declare a variable inside an if-statement, then it will 'fall out of scope' when the if-statement ends. If you want the value to persist outside the if-statement, then you need to declare the variable outside and then modify it within the if-statement. Alternatively, have a data structure (declared outside if-statement) like an array or List that you set/add to inside the if-statement.
I have made an public array and wanted to make the items manually in the editor, but I wouldn't want to show the variables in the editor which had no use. So I want to be able to have some variables specified by which type it has.
Then writing a custom inspector for it is suitable tactic and nulling unneeded variables
Let me see if I understand you correctly...
You want to have a system whereby you have a collection of objects, but you don't know (before the game begins running) exactly what type these objects are, only that they are all 'Items'. If it is a 'Resource' Item, then it has a Texture2D, but if it is a 'Weapon' then it has an int. Is this correct?
Answer by Itaros · Oct 23, 2014 at 12:52 PM
Use C# and polymorphism:
public abstract class item{
public string name;
}
public class resource:item{
public Texture2D texture;
}
public class weapon:item{
public int count;
}
Your answer
Follow this Question
Related Questions
Using an enum in a class constructor. 1 Answer
Enum style variable 2 Answers
Finding the length of an enum in a different class 1 Answer
Unknown identifier: 'types'. js(38,61) 1 Answer
Logic and datatype existential problem 2 Answers