- Home /
How can I make more than one AI entity prefab to move or activate?
Hi I have finally completed my own AI system for the customers in my game but I can only get it to work on one prefab in the scene. Even if I duplicate the customer prefab only one will move. I need the prefabs to act independently as well. How can I do this?
var waypoint : Transform;
var currentWaypoint : int;
var agent : NavMeshAgent;
var script1 : SlideOutMenu;
var activeWaypoints : Transform[];
var inactiveWaypoints : Transform[];
var masterWaypoints : Transform[];
var startMoving = false;
var selectedMenuItem : int;
var subtractedBurgers : int;
var subtractedGlucks : int;
var subtractedBopsters : int;
function Awake(){
agent = gameObject.GetComponent.<NavMeshAgent>();
agent.speed = Random.Range(2.5, 5);
script1 = gameObject.Find("GUIElements").GetComponent(SlideOutMenu);
}
function Start(){
inactiveWaypoints[1] = masterWaypoints[1];
inactiveWaypoints[2] = masterWaypoints[2];
inactiveWaypoints[3] = masterWaypoints[3];
inactiveWaypoints[4] = masterWaypoints[4];
activeWaypoints[5] = masterWaypoints[5];
inactiveWaypoints[6] = masterWaypoints[6];
inactiveWaypoints[7] = masterWaypoints[7];
inactiveWaypoints[8] = masterWaypoints[8];
inactiveWaypoints[9] = masterWaypoints[9];
inactiveWaypoints[10] = masterWaypoints[10];
inactiveWaypoints[11] = masterWaypoints[11];
inactiveWaypoints[12] = masterWaypoints[12];
inactiveWaypoints[13] = masterWaypoints[13];
}
function Update () {
TurnOnEntity();
if(startMoving == true){
waypoint = activeWaypoints[currentWaypoint];
agent.SetDestination(waypoint.position);
}
}
function OnTriggerExit(other : Collider){
if (other.name == "0 Start"){
currentWaypoint = Random.Range(2, 3);
}
}
function OnTriggerEnter (other : Collider) {
EatMeal();
if (other.name == "0 Start"){
currentWaypoint++;
}
if (other.name == "1"){
script1.globalMoney += 50;
WaitForSoda();
}
if (other.name == "2"){
selectedMenuItem = Random.Range(1, 4);
if(selectedMenuItem == 1 && script1.totalBurgers >= 1){
subtractedBurgers += Random.Range(1, script1.totalBurgers);
script1.totalBurgers -= subtractedBurgers;
script1.globalMoney += subtractedBurgers * 10;
subtractedBurgers = 0;
}
if(selectedMenuItem == 2 && script1.totalGlucks >= 1){
subtractedGlucks += Random.Range(1, script1.totalGlucks);
script1.totalGlucks -= subtractedGlucks;
script1.globalMoney += subtractedGlucks * 15;
}
if(selectedMenuItem == 3 && script1.totalBopsters >= 1){
subtractedBopsters += Random.Range(1, script1.totalBopsters);
script1.totalBopsters -= subtractedBopsters;
script1.globalMoney += subtractedBopsters * 20;
}
WaitForFood();
}
if (other.name == "4 End"){
Destroy(gameObject);
}
}
function WaitForSoda(){
yield WaitForSeconds(2);
if (activeWaypoints[3] == masterWaypoints[3]){
currentWaypoint++;
}
if (activeWaypoints[12] == masterWaypoints[12]){
currentWaypoint = Random.Range(12, 13);
}
if (activeWaypoints[6] == masterWaypoints[6]){
currentWaypoint = Random.Range(6, 8);
}
if (activeWaypoints[9] == masterWaypoints[9]){
currentWaypoint = Random.Range(9, 11);
}
}
function WaitForFood(){
yield WaitForSeconds(5);
if (activeWaypoints[12] == masterWaypoints[12]){
currentWaypoint = Random.Range(12, 13);
}
if (activeWaypoints[6] == masterWaypoints[6]){
currentWaypoint = Random.Range(6, 8);
}
if (activeWaypoints[9] == masterWaypoints[9]){
currentWaypoint = Random.Range(9, 11);
}
}
function EatMeal(){
if (currentWaypoint >= 6 && currentWaypoint <= 13){
yield WaitForSeconds(Random.Range(6, 13));
currentWaypoint = 5;
}
}
function TurnOnEntity(){
if (activeWaypoints[1] == masterWaypoints[1]){
startMoving = true;
}
if (activeWaypoints[2] == masterWaypoints[2]){
startMoving = true;
}
if (activeWaypoints[3] == masterWaypoints[3]){
startMoving = true;
}
}
Is this script attached to all the prefabs? If these are prefabs, where's the instantiate and is it doing/setting anything?
Yea this script is what controls the AI and it is attached to all prefabs. The instantiate is located on another script and when the player makes an upgrade a new customer is instantiated by cloning the existing prefab in the scene. The prefab will clone itself in the scene but wont update any of its values. Here is part of the script that controls the cloning
var r_UpgradeObjects: GameObject[];
var script : GlobalTime;
var waypointScript : NavigateWaypoints;
var customerObj : GameObject;
var debt = false;
var show$$anonymous$$enuPos : float[];
var show$$anonymous$$enuSize : float[];
function Update() {
waypointScript = gameObject.FindWithTag("Player").GetComponent(NavigateWaypoints);
if (global$$anonymous$$oney <= -100) {
Application.LoadLevel(4);
}
}
//Button Set 2
if (b_SubsetButtons[1] == true) {
//Table W/ Two Chairs
if(b_HideButtonSet[0] == true){
if (GUI.Button(buttonSet2Rect1, b_ButtonText[b_ButtonTextHolder]) && b_ButtonTextHolder == 0) {
waypointScript.inactiveWaypoints[12] != waypointScript.masterWaypoints[12];
waypointScript.activeWaypoints[12] = waypointScript.masterWaypoints[12];
waypointScript.inactiveWaypoints[13] != waypointScript.masterWaypoints[13];
waypointScript.activeWaypoints[13] = waypointScript.masterWaypoints[13];
Instantiate(customerObj);
global$$anonymous$$oney -= 40;
r_UpgradeObjects[0].SetActive(true);
b_ButtonTextHolder = 10;
b_HideButtonSet[0] = false;
}
}
Put some debug into TurnOnEntity to see if the 2nd clone/preab actually gets there and changes set$$anonymous$$oving = true
The second prefab doesnt change any of its values and wont seek out waypoints. I presume this is because in my instatiate script its not detecting the clone.
I don't see anything that's looking for children of "Customers" but there's a lot of code here and I might be missing it. If the clone needs to be a child of Customers, you can change your Instantiate to be
newObj = Instantiate;
newObj.transform.parent = Customers
and also if your scripts are looking for something named "Customer"
newObj.name = "Customer"
although then you have to be sure your scripts are thinking the name "Customer" is unique