- Home /
Adding a variable to instantiated object causes infinite objects to spawn.
function OnCollisionEnter(collision : Collision) { transform.position.x = 0; if (collision.relativeVelocity.magnitude > 1) { health -=15 * damagePower; }
if(health < 0)
{
var temp:Quaternion = transform.rotation;
var crate = Instantiate(blox, transform.position, temp);
crate.transform.scale -= 0.5;
Destroy (gameObject);
}
} Using this script I am instantiating a smaller box when the large one has it's health go below 0. Works fine, makes one object, and doesn't crash the system or anything when I remove "crate.transform.scale -= 0.5;". In fact, if I remove the transform.scale and try to pass on
any variable to the object it seems to try creating an infinite number of objects.
why? also, how do I make it actually work? thanks
edit:
I tried it with the new script you gave me. I edited out the collision point since what I need is the new block to spawn at the same location and angle as the one just destroyed. I also forgot to reattach my target in the engine to the blox:GameObject. Once I got it sorted out I getting the scale to work, but not passing any script defined variables. This is the script I am using on both the original block and the same block with the same script attached as the spawn:
var health : int = 16; var blox : GameObject; var blockType:int = 3; function Start() { if(blockType >0) { health = 100 * blockType; } else { health = 100; } } function OnCollisionEnter(collision : Collision) {
transform.position.x = 0; if (collision.relativeVelocity.magnitude > 5) { health -=100; // for now just kill the block with one hit }
if (health <= 0){ var rot : Quaternion = transform.rotation; var pos : Vector3 = transform.position; var crate = Instantiate(blox, pos, rot); crate.transform.localScale = Vector3(0.5,0.5,0.5); //change the new instantiated block to blockType 6 only using that as and example, // it normally would be crate.blockType = blockType; to pass on //the same blockType var this block used crate.blockType = 6; // Destroy the original crate Destroy (gameObject); }else{ //Debug.Log("Nothing spawned"); } } I have several blocks in the scene and each blockType of the original can be set in the engine, but the new block spawned is from the same prefab they all use so a large healthy block spawns the same blockType as a smaller weak block instead of passing on it's block type used in engine. This is my current error in the console "MissingFieldException: Field 'UnityEngine.GameObject.blockType' not found." I am trying to use the same block prefab and the same script on the object for multiple block types so I need to pass on the blockType var.
I have tried blox as a Transform and GameObject of a prefab in the engine.
Answer by Lachee1 · Aug 09, 2012 at 11:56 PM
ofset the blox, your spawning it inside the object, making it run through the collision again, offseting it should fix this. EG:
function OnCollisionEnter(collision : Collision)
{
transform.position.x = 0;
if (collision.relativeVelocity.magnitude > 1)
{
health -=15 * damagePower;
}
if(health < 0)
{
var temp:Quaternion = transform.rotation;
var crate = Instantiate(blox, transform.position + Vector3(0,1.5,0), temp);
crate.transform.scale -= 0.5; //needs to be crate.transform.localScale -= Vector3(0.5,0.5,0.5);
Destroy (gameObject);
}
}
var crate = Instantiate(blox, transform.position + Vector3(0,1.5,0), temp);
Now on the spawned object, add a script taking 1.5 away from the Y axis, but add a slight delay, make it yield WaitforFixedUpdate(); after the Start function.
Doesn't seem to work. I tried offsetting the blox without actually setting it back to where it should be and it still explodes with infinite boxes. I am gettting an error. "Object reference not set to an instance of an object" any idea what's causing that and could it be the issue?
is the blox scaling? that might be the issue, i think you have to do local scale EG: transform.localScale unity doesn't have just scale
You also need to change it so it take a Vector3, i have edit the answer to show you what to do.
I tried taking the scale off completely and tried just changing the health variable in the new instance.
crate.health = 50;
and it still tries replicating ad infinitum. I tried it this way since I wasn't sure about needing a Vector3 on scale.
I just turned on "error pause" in the console and found this when it tries to run the script when I try changing the health variable.
$$anonymous$$issingFieldException: Field 'UnityEngine.Transform.health' not found.
Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.FindExtension (IEnumerable`1 candidates)
This leads me to believe that I can't do it like I saw in samples online on how to pass a variable change to an instantiated object by just adding it in like that.
Does the var need to be defined by type? I have seen people use var item:GameObject = Inst.....
The script won't even run for me using that though. I get a "cannot convert Transform to GameObject" error
So anyway 'UnityEngine.Transform.health' seems to be my main issue right now.
can you give me the whole script? the one that creates the object, and the one the object uses (if any)
you maybe accidently trying to get health form the transform.
What did you declare Blox as? a transform of a gameobject,
Answer by Lachee1 · Aug 11, 2012 at 10:20 AM
You need to get the components, it wont treat it like its a varable built into the gameobject type. you need crate.getComponent("ThisScript").type = 6;, like the example below.
var health : int = 16;
var blox : GameObject;
var blockType:int = 3;
function Start()
{
if(blockType >0)
{
health = 100 * blockType;
}
else
{
health = 100;
}
}
function OnCollisionEnter(collision : Collision) {
transform.position.x = 0;
if (collision.relativeVelocity.magnitude > 5)
{
health -=100; // for now just kill the block with one hit
}
if (health <= 0){
var rot : Quaternion = transform.rotation;
var pos : Vector3 = transform.position;
var crate = Instantiate(blox, pos, rot);
crate.transform.localScale = Vector3(0.5,0.5,0.5);
//change the new instantiated block to blockType 6 only using that as and example,
// it normally would be crate.blockType = blockType; to pass on
//the same blockType var this block used
crate.gameObject.GetComponent("ThisScriptName").blockType = 6;
// Destroy the original crate
Destroy (gameObject);
}else{
//Debug.Log("Nothing spawned");
}
}
I had just found this last night and tried it. I am getting an error though "Object reference not set to an instance of an object".
ok, got it to work
var temp:Quaternion = transform.rotation; var oneBlox : GameObject; oneBlox = Instantiate (blox, transform.position, temp); var bloxScript = oneBlox.GetComponent("Blox"); // name of script without the ".js" at the end. bloxScript.blockType = 6; // var blockType set to 6The main issue I was having was that the prefab I was instantiating was actually a group of blocks each with the script and I couln't change the variable in each one. Any way to do this to a child of the instance?
Your answer
![](https://koobas.hobune.stream/wayback/20220613075425im_/https://answers.unity.com/themes/thub/images/avi.jpg)