- Home /
Push once then keep updating.
As the title speaks for itself, I'll explain it more properly.
I have an array wich contains positions of certain objects. I don't want to keep pushing more and more of the same objects in the array so I put the push in a Start function. But now the positions in the inspector (from a Vector3 array wich is filled with the normal array) arentio't changing. Obvious cause its in a Start function.
Now for the question: How can I push these objects in the array, and then keep the positions updating?
Help is really appreciated :) Thanks in advance!
For those who need the script:
//This script is created and made by Freel4tte. //Anyone who uses this script illigal or without permission will //get hunted down by zombies on tricycles and ninja bears with laser eyes. //You have been warned.
var filteredEnemies = Array(); //all other objects var posEnemies = Array(); //positions of other objects
var showEnemies : GameObject[]; var getPos : Vector3[];
@HideInInspector var enemies : GameObject[];
function Start(){ seeEnemies(); }
function Update(){
getPos = posEnemies;
}
function seeEnemies():GameObject{
enemies = GameObject.FindGameObjectsWithTag("enemy");
for (var currEnemy in enemies){
if (currEnemy == gameObject)
continue;
filteredEnemies.Push(currEnemy);
posEnemies.Push(currEnemy.transform.position);
}
showEnemies = filteredEnemies;
}
Long question short: if the seeEnemies function produces a NEW enemy you want it to be pushed into the array, if it's not NEW you want nothing to happen. Correct?
I don't really get your comment. Do you mean when an enemy is spawned in runtime?
Is it that when ever currEnemy is different you want it to be pushed into the array - or is it that at start you want the currEnemy to be pushed into filteredEnemies - but posEnemies get's pushed constantly?
Answer by Joshua · Apr 28, 2011 at 01:35 PM
If I understand your question correctly, in your update function you want to loop through the array of filteredEnemies and update the posEnemies array with the new positions?
if so, in the Update function:
for(i=0;i<filteredEnemies.length;i++) {
posEnemies[i] = filteredEnemies[i].transform.position
}
That's what this does. In the update function it constantly changes the array of enemypositions to their current position..
This doesnt work. The positions are pushed in the array when the game starts. This happens only once because if it where in update, the array will get more and more of the same positions. When the game starts it checks for the already placed enemies in game, and stores them in an array. An other array gets the positions of these objects. Right now there are three of them. But I want to update this position array so I can track the exact position in the inspector.
What I'm trying to make here is when an enemy comes near an position in the array, it stays a couple of units away from it. Thanks
Why doesn't this work? o.O the positions in the position array are being replaced with the current positions, as you wanted? Do you also need to check for need enemies?
Never$$anonymous$$d... Your way works. I forgot to give the array in the inspector the values of the posEnemies. Thanks for your time and help my man!
Your answer