I am making a pac man game, and I ran into a problem when tyring to have the ghosts leave the starting area. If you played the actual game, the red one goes first, then a second later the next etc… I am having troubles getting this whole thing to play out right.
The problem is this:
I have a counter that adds one when the ghost hits the trigger outside of the area. when it is at 0, the red goes etc…I also have a script that is attached to each ghost, the same script. There is a variable on that script called goUp which move the ghost up to the next trigger. What I am having problems with is getting only the Red Ghost to use the goUp variable, and when it hits the trigger, the Pink one does so etc… Here is my code if it will help:
function Update(){
leave();
if(star){
direction = Random.Range(0, 2);
if(direction == 0){
goRight = true;
goUp = false;
star = false;
}else if(direction == 1){
goLeft = true;
goUp =false;
star = false;
}
}
function leave(){
if(startTime == 0){
goUp = true;
}
}
function OnTriggerStay( other : Collider ){
if(other.gameObject.tag == "start"){
star = true;
startTime += 1;
}
}
right now your goUP variable isnt really attatched to anything - its pretty generic which isnt really what you want
the way i would suggest doing it is creating an array of ghosts and then getting the script’s goUp variable for each one
something like this
var ghsts[] : Ghosts[];
***fill it with your ghosts***
var counter =0;
if(star){
ghsts[counter].getComponent("scriptName").goUp = true;
if(direction == 1){
counter +=1;
}
}
its rough but it should get you started
This seems like it may work, but here is small error I got when trying it:
var ghsts[] : Ghosts[];
seems to not work
I don’t know why it doesn’t. It tells me to add a semi colon at the end, but there already is.
try:
var ghosts : Ghosts[];
(remove the first brackets)
yet again… It said this:
var ghosts : Ghost[ ];
The name ‘Ghost’ does not denote a valid type.
You need to declare Ghosts (or Ghost?) as a type, which means making a class for it.
class Ghost{
var data1;
var data2;
}
var ghosts : Ghost[ ] = new Ghost[ ];
Thought my JS is a little rusty. If you were using c#, I’d say use the collections library to make your own typed list (very fast, with JS Array functionality).
ok sorry - this is what you want for JS
var ghsts : GameObject[] = new GameObject[numghosts];
Thanks guys! It works perfectly now 