- Home /
Can't use method from custom class during foreach
In my program, I created a class, 'Predator' that creates a GameObject and has methods, specifically one to move the object. Here that is:
public Predator(float speed, float splitsize) {
this.speed = speed;
this.splitsize = splitsize;
PredObjPrefab = Resources.Load("Predator") as GameObject;
PredObj = GameObject.Instantiate(PredObjPrefab) as GameObject; // Instantiate
PredFolder = GameObject.Find ("PredFolder"); // Storing Predator in folder to not make a mess
FoodFolder = GameObject.Find ("FoodFolder");
PredObj.transform.SetParent (PredFolder.transform);
rb = PredObj.GetComponent<Rigidbody> ();
}
public void Move() {
// Find closest food to self
distance = 100000;
foreach (Transform child in FoodFolder.transform) {
tempdistance = Vector3.Distance (child.transform.position, PredObj.transform.position);
if (tempdistance < distance) {
ClosestFood = child.gameObject;
distance = tempdistance;
}
}
// Move towards food by changing self velocity
PredObj.transform.LookAt(ClosestFood.transform);
rb.velocity = speed*PredObj.transform.forward;
}
Every Predator I spawn is stored in PredFolder, a GameObject. In my main file I have started with just two Predators. I want to access them by iterating through the children of PredFolder. I attempt to do this with a foreach.
void Start () {
PredFolder = GameObject.Find ("PredFolder");
for (i = 0; i < 100; i++) { // Initial food spawn so that the first predators don't starve
Foodtemp = new Food (new Vector3 (Random.Range (-500, 500), 1, Random.Range (-500, 500)));
}
catchTime = Time.time + 1; // Time must pass one second before spawning more food
Predtemp = new Predator (10, 10); // Initial predator (speed, splitsize)
Predtemp.PredObj.transform.position = new Vector3 (0, 0, 0);
Predtemp = new Predator (10, 10); // Initial predator (speed, splitsize)
Predtemp.PredObj.transform.position = new Vector3 (10, 0, 0);
}
// Update is called once per frame
void Update () {
if (Time.time >= catchTime) { // Spawn food every second
Foodtemp = new Food (new Vector3 (Random.Range (-500, 500), 1, Random.Range (-500, 500)));
catchTime = Time.time + 1;
}
foreach (Transform child in PredFolder.transform) {
child.Move ();
}
}
The last statement is clearly not working because child is a Transform, which wouldn't have the method Move() from my Predator class. I can't figure out how to change the parameters of the foreach so that each child is a Predator. How do I properly execute methods of each Predator in the folder?
Answer by Casiell · Nov 06, 2019 at 10:54 AM
So first of all if your Predator class doesn't inherit from MonoBehavior, your approach won't work. If it does, I would still call this a bad approach.
Why rely on hierarchy when you can just store all your Predator objects in a list and iterate it?
Answer by xxmariofer · Nov 06, 2019 at 11:57 AM
foreach (Transform child in PredFolder.transform) {
child.GetComponent<Predator>().Move();
}
The problem with this is that Predator is not a $$anonymous$$onoBehavior so it's not attached to gameobjects, so you can't access it with GetComponent.
true, i was reading too fast and didnt realize he was using constructors, then his issue is with oop and c#, probably doesnt know that both predators that he has created are already GC before reaching the update, because he is not saving them anywhere
List<Predator> predatorList = new List<Predator>();
void Awake(){
predatorList.Add(new Predator (10, 10));
predatorList[0].PredObj.transform.position = new Vector3 (0, 0, 0);
predatorList.Add(new Predator (10, 10));
predatorList[1].PredObj.transform.position = new Vector3 (0, 0, 0);
}
void Update(){
foreach (Predator predator in predatorList) {
predator.$$anonymous$$ove ();
}
}
Your answer
Follow this Question
Related Questions
Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do? 0 Answers
Why the variable don't change your value? 1 Answer
Accessing a method / void from another class? 0 Answers
Passing a class instance to a method 1 Answer
Call method from attached Gameobject 1 Answer