- Home /
How can I make a list of Classes or Scripts?
Sorry for the confusing title, I'm new and not quite sure what I'm doing and what everything is called. And I apologize for a long and probably confusing question.
But basically I have an object(Zombie/green circle(s) in the scene) that needs to face and move to another object(Window/Barrier/the grey bar in the scene). And eventually the Window/Barrier will be destroyed and the Zombie will then target a different object(Survivor/red circle in the scene).
The Zombie has a script that makes the Zombie search for all the Windows/Barriers and Survivors in the game currently, and place them into two lists. The script will then sort the two lists by distance from the Zombie itself. The script will then set the closest Survivor and Window/Barrier as its targets.
So there will be a Survivor target, and a Window/Barrier target.
The Zombie GameObject will not use a method from its own script to switch from the Window/Barrier target to the Survivor target, instead, it will use a script from the Window/Barrier GameObject.
If you look in the inspector, you can see the Targets list, the elements are empty. My Objective is to find all the Zombies and add them to the list so all Zombies in the scene can access the script in the Window/Barrier GameObject.
If you look right below Element 1 in the inspector, There is a public Class (Zombie Controller) called Zombie Method. I have assigned one of the Zombie GameObjects into the public Class(is that what it is?) and only that Zombie behaves as intended.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class BarrierHealth : MonoBehaviour {
public List<ZombieController> targets; //Here I make the list for the Classes(Is that right?)
public ZombieController zombieMethod; //This is basically what I want a list of. I need to assign more than one of these
public SurvivorController survivorMethod;
public GameObject barrier;
public CircleCollider2D survivor;
public int barHealth;
public int curBarHealth;
void Start()
{
curBarHealth = barHealth;
Invoke ("AddTarget", 0);
}
public void AddAllZombies() //This method will find all the zombies and add them to the list
{
GameObject[] go = GameObject.FindGameObjectsWithTag ("Zombie");
// Here I need to add the zombies to the list that contain the public Class (ZombieController)
foreach (GameObject zombie in go)
AddTarget (zombie); //error says this is supposed to be a .transform
}
public void AddTarget(ZombieController zombie) //Not sure what I was doing here
{
targets.Add (zombie);
}
void OnTriggerStay2D(Collider2D survivor)
{
if (survivor.gameObject.tag == "Survivor")
{
survivorMethod.InvokeLookAtZombies();
survivorMethod.CancelInvokeMoveToTarget();
// methods that are use by the Window/Barrier and are in the Zombie GameObject
Debug.Log ("SurvivorIsInTrigger");
}
}
void OnTriggerExit2D(Collider2D survivor)
{
if (survivor.gameObject.tag == "Survivor")
{
survivorMethod.CancelInvokeLookAtZombies();
Debug.Log ("SurvivorExitedTrigger");
}
}
void Update()
{
if (curBarHealth <= 0)
{
barrier.SetActive (false);
barrier.tag = "DestroyedBarrier";
}
}
public void AttackBarrier()
{
curBarHealth -= 1;
if (curBarHealth <= 0f)
{
Debug.Log ("BarrierDestroyed");
Invoke("CancelAttackBarrier", 0);
Invoke ("SetBarrierAsDestroyed", 0);
InvokeRepeating ("AggroSurvivor", 0, .01f);
CancelInvoke("AttackBarrier");
CancelInvoke("AggroBarrier");
}
}
} //There's more of the script that I didn't show because it was irrelevant
Answer by YoungDeveloper · Aug 17, 2015 at 12:47 PM
public List<ZombieController> targets;
List stores classes of type ZombieController
GameObject[] go = GameObject.FindGameObjectsWithTag ("Zombie");
foreach (GameObject zombie in go){
//AddTarget (zombie); //You are trying to add GameObject, but it expects ZombieController
ZombieController controller = go.GetComponent<ZombieController>();
if(controller != null) targets.Add (controller);
else Debug.LogError("GameObject with tag Zombie does not contain component ZombieController");
}
Thank you very much with the help, but now '.GetComponent' doesn't seem to be recognized or work
It's a typo. You are calling GetComponent() on the whole array (go) ins$$anonymous$$d of on the iterator variable (zombie).
Answer by stepan-stulov · Aug 17, 2015 at 12:46 PM
Simply replace the field that stores the reference to a zombie with a field that stores a list that stores multiple references to zombies.
public ZombieController zombieMethod;
// becomes
public List<ZombieController> zombieControllers;
Now fill the list with any mechanism of your choice and also process the list using the foreach loop.
PS: Speaking of terminology, you don't want a list of classes or scripts, you want a list of instances of the ZombieController class whose source code is stored in a script file. Class and script are two terms that are used almost 100% interchangeably within the Unity engine. I would say a script is Unity's narrower case of C#'s class. Every script is going to be a class, but not every class could be qualified as a script, only those directly participating in Unity routines (serialization, scene graph, etc.) are I think.
Answer by Chrisasan · Aug 17, 2015 at 12:50 PM
to create an array of scripts in Java Script: var scr : scriptName[]; If the scriptName[] is public then you can use the editor to define how many of those objects you want. If it is private then you will have to allocate them.
scr=new scriptName[amount]; after that you would have to still allocate them. for(var scan=0;scan < scr.Length;scan++) scr[scan]=new scriptName();
When you tried to access go.GetComponent() you forgot to tell it which go you wanted. Exp: go[arraynumber].GetComponent();
Ok now to call a method on the ZombieController Script would I have to use something like zombieControllers.Element[].$$anonymous$$ethodThatIWantToUse();