Hi, I am trying to make the AI not move for the first 3 seconds. As in 3 2 1 go kind of thing. Ive used the script below but it only works if only one of the cars is tagged AI. If I TAG them all “AI” then they all carry on moving. Thanks in advance for any help.
var guiCountDown : GUIText;
var countMax : int;
private var countDown : int;
function Start(){
guiCountDown.enabled=true;
GameStart();
}
function Update () {
}
function GameStart(){
var car = GameObject.FindWithTag ("AI");
var drivingScript = car.GetComponent("AICar_Script");
drivingScript.enabled=false;
for (countDown = countMax; countDown>0;countDown--){
guiCountDown.text = countDown.ToString();
yield WaitForSeconds(1);
}
guiCountDown.enabled=false;
drivingScript.enabled=true;
}
As you said “Probably me!”
You used GameObject.FindWithTag ("AI")
which will return only one GameObject, you may want GameObject.FindGameObjectsWithTag ("AI")
instead.
replace lines 15-17 :
var car = GameObject.FindGameObjectsWithTag("AI");
var drivingScript;
foreach (var key in drivingScript) {
drivingScript[key] = car[key].GetComponent("AICar_Script");
drivingScript[key].enabled = false;
}
and line 27 browse the drivingScript Array
(Sorry if the code is wrong I’m used to C#)
You should use GameObject.FindGameObjectsWithTag since you have more than one object.
[GameObject.FindGameObjectsWithTag][1]
Basically, it fills an array with the tagged GameObjects and you can iterate through them using a for loop. your script might go like this:
var cars : GameObject[];
cars = GameObject.FindGameObjectsWithTag("AI");
for (var i = 0; i < cars.Length; i++) {
cars*.GetComponent("AICar_Script").enabled = false;*
}
for (countDown = countMax; countDown>0;countDown–){
guiCountDown.text = countDown.ToString();
yield WaitForSeconds(1);
}
guiCountDown.enabled = false;
for (var ii = 0; ii < cars.Length; ii++) {
cars[ii].GetComponent(“AICar_Script”).enabled = true;
}
EDIT: Whoops. Got ninja’d.
EDIT2: Fixed acc. to ThomLaurent
[1]: Unity - Scripting API: GameObject.FindGameObjectsWithTag
Thank you once again. I managed to fix it in the end. I will post this here for incase anyone finds it usefull in the future. Must be attached to the camera and gameobjects must be tagged.
var guiCountDown : GUIText;
var countMax : int;
private var countDown : int;
function Start(){
guiCountDown.enabled=true;
GameStart();
}
function GameStart()
{
var cars : GameObject[];
cars = GameObject.FindGameObjectsWithTag("AI");
for (var i = 0; i < cars.Length; i++) {
cars*.GetComponent("AICar_Script").enabled = false;*
}
for (countDown = countMax; countDown>0;countDown–){
guiCountDown.text = countDown.ToString();
yield WaitForSeconds(1);
}
guiCountDown.enabled = false;{
for (var ii = 0; ii < cars.Length; ii++) {
cars[ii].GetComponent(“AICar_Script”).enabled = true;
}
for (countDown = countMax; countDown>0;countDown–){
guiCountDown.text = countDown.ToString();
yield WaitForSeconds(1);
}
guiCountDown.enabled=false;
}
}
(basically stops the AI from moving until the player is allowed.)