- Home /
Passing a variable to the child of Instantiated object?
I'm working on a destructible environment and when a wall breaks 4 equal parts fall away. In script I want to pass a variable to each of the 4 child instances. I wouldn't think this would work properly if the prefab of the 4 parts were in a group when the script with the variable is attached to each wall piece not the actual prefab.
I was looking at something like this example I found in the questions area.
Bullet zBullet = Instantiate(bullet, transform.position, Quaternion.identity) as Bullet;
zBullet.playerNum = playerNumber;
But I think this would just make the zBullet have the variable not any child of it.
Any way to pass a variable to a child of an Instantiated prefab? thanks
Answer by aldonaletto · Aug 07, 2012 at 04:36 AM
If the four parts have the same script (let's suppose it's called ChildScript.cs) you can do something like this:
public GameObject wallPrefab; // wall prefab ... // Instantiate the wall: GameObject wall = Instantiate(wallPrefab, ...) as GameObject; // for all children in wall... foreach (Transform child in wall.transform){ // try to get the child script: ChildScript script = child.GetComponent< ChildScript>(); if (script){ // if it has such script... script.someVar = someValue; // set its variable } } ...
EDITED: I thought your code was in C# - there goes the JS version:
var wallPrefab: GameObject; // wall prefab ... // Instantiate the wall: var wall: GameObject = Instantiate(wallPrefab, ...); // for all children in wall... for (var child: Transform in wall.transform){ // try to get the child script: var script: ChildScript = child.GetComponent(ChildScript); if (script){ // if it has such script... script.someVar = someValue; // set its variable } } ...
Doesn't seem to work for me. GameObject wall = Instantiate tells me I need a ; in the line, but using var wall = Instantiate works.
my main issue though is that whenever I try passing a variable to the child or even the prefab itself the engine freezes and it looks like it's trying to Instantiate infinite wall pieces.
function OnCollisionEnter(collision : Collision) {if(health < 0) { var temp:Quaternion = transform.rotation; var wall = Instantiate(blox, transform.position, temp); wall.health=100; Destroy (gameObject); } }the wall.health=100; is where I get the trouble of it crashing. I can remove that line and all works out ok. I can put anything there with "wall." and it seems to crash. a transform.rotation seems to not crash it, but a transform.scale does. I can't get my head around what the issue could be.
I need it in JS, that looks like C. Would you know how to convert it to js? $$anonymous$$ainly the foreach section. I'm pretty sure I can do the rest.
I got it to work, but I don't think I am using a parent child relationship anymore. here's what I did:
var oneBlox : GameObject;
oneBlox = Instantiate (targetBlock, tempPosision, tempRotation);
var bloxScript1 =
oneBlox.GetComponent("Blox"); // name of script without the ".js" at the end.
bloxScript1.blockSize = 2; //edit the blockSize variable in the instantiateed object
bloxScript1.blockType = blockType; //edit the blockType variable in the instantiateed object
Your answer
Follow this Question
Related Questions
Any ideas on a simple AI script that doesn't use any drag/drop variables 1 Answer
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
Spawn Shield to all objects 2 Answers
Instantianig a teleporter prefabs with destination variables 1 Answer
Give prefab a parent 2 Answers