Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by redteardrop · Aug 09, 2012 at 03:29 PM · instantiatecrashvariables

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image redteardrop · Aug 10, 2012 at 05:12 PM 0
Share

I have tried blox as a Transform and GameObject of a prefab in the engine.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image redteardrop · Aug 10, 2012 at 12:25 AM 0
Share

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?

avatar image Lachee1 · Aug 10, 2012 at 12:28 AM 0
Share

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.

avatar image redteardrop · Aug 10, 2012 at 04:26 AM 0
Share

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.

avatar image Lachee1 · Aug 10, 2012 at 06:11 AM 0
Share

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,

avatar image
0

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");
 
   }
 
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image redteardrop · Aug 11, 2012 at 01:55 PM 0
Share

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".

avatar image redteardrop · Aug 11, 2012 at 03:35 PM 0
Share

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 6
The 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with variables across scripts 0 Answers

How to access varibles on a newly instantiated object 2 Answers

Crash when using GameObject.Instantiate 2 Answers

OnCollisionEnter2D() Instantiate Causing an Infinite Loop 0 Answers

IL2CPP Crash on Instantiate(gameObject) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges