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