- Home /
Changing a variable on an instantiated prefab clone once created
Note: Would prefer code snippets in Java, but I can read C# if you prefer to use that.
I'm creating a 2D sidescroller with 4.3+ and am attempting to use instantiated blocks from 4 prefabs to create the ground and obstacles the player is going to encounter. These move across the screen when the player is "moving" but the player stays in the middle of the screen. The issue is the blocks hit the player and push him off the screen. If I use a linecast or collider I can get a single block to stop moving, but I want all blocks on the screen to stop moving if the player collides with any of them to simulate the player stopping.
Now I can get the instantiated objects to spawn moving or to spawn stationary, but I cannot get them to switch states once created. I've also tried using several variations of arrays to check all objects with the same tag and move them that way, but this has yielded no results nor does it generate errors.
Example of both codes:
if(playerRunning == true && groundSpawnTimer >= 5)
{
var dirtBlock = Instantiate(GroundBlock, new Vector2(0,-1.1), Quaternion.identity);
var dirtMove : BlocksMove = dirtBlock.GetComponent(BlocksMove);
//dirtMove.blockMove = true; //Commented out as this spawns blocks moving
groundSpawnTimer = 0;
}
var tiles = GameObject.FindGameObjectsWithTag("Ground"); //Attempted array to move spawned blocks
for(var ground in tiles)
{
if(playerRunning == true)
{
//ground.transform.position -= transform.right*7*Time.deltaTime; //Wasn't sure what I was supposed to reference here. Tried several. None worked.
dirtBlock.transform.position -= transform.right*7*Time.deltaTime;
}
I have all my components attached to a single game controller except the code to move the blocks which is contained in the prefabs. If there's a way to move this code to the controller to make it easier that's fine, but my main concern is stopping all blocks from moving upon collision (or setting playerRunning == false on collision to stop the blocks).
The only thing I've not tried to spawning new blocks rather than instantiating prefabs, but I think this would present the same issue in the long run.
Answer by C.IV · Feb 24, 2014 at 01:43 PM
I figured out a way via checking against the player's own code how to stop instantiated objects or update their status based on the player's status. By using the player as a set object (already in the scene) I placed the following code into the prefab block objects:
function Update () {
var playerState : playerController = GameObject.FindGameObjectWithTag("Player").GetComponent(playerController);
if(playerState.running == true)
{
transform.position -= transform.right*blockSpeed*Time.deltaTime;
}
}
By having every instantiated object seek out the gameobject tagged as "player" and then check to see if its running state was true (set by the game controller when the player pressed the run key) each instantiated object can then update itself without having to refer back to the prefab it was created from.
On larger games or if you have many hundreds of objects this is probably a bad way to do it, but it works for small numbers of objects without affecting performance.
Your answer
Follow this Question
Related Questions
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer
Instantiated GameObject's components are disabled 3 Answers
Prefab clones behaviour 0 Answers
Instantiate GameObject at Random time is not working 1 Answer
How can I get the x position for the left(and right) of the screen? 2 Answers