- Home /
Declaring Variables in an Array of a Class
I want to be able to set the variables in an array of a custom class, like so:
//Class of an enemy in the level, and whether they are in range or sight
public class Enemy {
public GameObject enemy;
public bool inRange;
public bool inSight;
}
//Array of enemies
public Enemy[] enemies;
//Find all enemy units in the level
void Awake () {
enemies.enemy = GameObject.FindGameObjectsWithTag ("Enemy");
enemies.inRange = false;
enemies.inSight = false;
}
Even if this is an invalid way to do this sort of thing, is there any way I can?
It would make more sense if the array was of GameObjects and that the Enemy class inherited from $$anonymous$$onobehaviour and was made a component of the GameObject, rather than having the GameObject an attribute of the Enemy.
So something like this
Warning code may not compile, not tested, just an idea
//Class of an enemy in the level, and whether they are in range or sight
public class Enemy : $$anonymous$$onoBehaviour {
//public GameObject enemy;
public bool inRange;
public bool inSight;
}
//Array of enemies
//public Enemy[] enemies;
public GameObject[] enemies;
//Find all enemy units in the level - better to deal with other objects after they have been initialized (Start)
void Start () {
enemies = GameObject.FindGameObjectsWithTag ("Enemy");
for each (GameObject go in enemies)
{
Enemy enmy = go.GetComponent<"Enemy">();
enmy.inRange = false;
enmy.inSight = false;
}
}
The above assumes you have used a tag called "Enemy" as well as having a class called Enemy
Not sure I agree; composition over inheritance is a good thing. Note that GetComponent will only work if Enemy both inherits from $$anonymous$$onoBehaviour (as it does here) AND is present on every Enemy.
Small correction to @richyrich 's good answer
i believe it's either
Enemy enmy = go.GetComponent<Enemy>();
// or
Enemy enmy = go.GetComponent("Enemy");
@Nose$$anonymous$$ills - well spotted. I've been dabbling in UnityScript today and got myself confused ;) For c# it should be first one
@Uldeim - when commenting/answering a question, I try to use mainly the OP's code, otherwise it serves little point. In the OP code, we have FindGameObjectsWithTag - now that returns GameObjects, not the custom class as was desired. The OP wanted to then edit the attributes. It therefore suggests that if the objects are GameObjects which have the same tag, they all have a shared behaviour. On that premise, it is fair to say they share at least one same script in order to share the behaviour. Removing the GameObject attribute simplifies code readability and the general concepts I was trying to convey. I made some assumptions and put forward a suggestion on that basis - hence a comment not an answer
However, given the OP stated that he wanted to know a way, even if 'invalid' we can all be right :D
Your answer
Follow this Question
Related Questions
Creating A Class Variable That Works With Any Class 0 Answers
Declaring a variable in Start funciton 3 Answers
Accesing custom class object from another script 1 Answer
How do I change public variables depending on which level? 1 Answer
Possible to use an array value as the name of a variable? 2 Answers