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
2
Question by EndBoss · Sep 14, 2010 at 03:58 AM · instantiatetransformspawnparent

how do i know where my instantiated object came from?

Ok so I have 3 GameObjects that are being used to instantiate objects. They are all called TargetSpawner_GO

I have the following script attached to them:

    function SpawnAnother(){
        var localObject : GameObject = Instantiate(myObject,transform.position, transform.rotation);
        localObject.GetComponent(moveTarget).speedLeft = myCounter + 2;
        localObject.GetComponent(moveTarget).speedRight = myCounter + (-2);
    }

When the instantiated object is killed I use the following script:

var deathClock : float = 5.0; var deathTime : float = 5.0; var startDeathClock = false; function Update () {

 deathClock -= Time.deltaTime;
 //Debug.Log(deathClock);

     if(startDeathClock){
         if(deathClock < 0){
             Destroy(gameObject);
             var myObject = GameObject.Find("TargetSpawner_GO").GetComponent(SpawnTarget);
             myObject.SpawnAnother();
         }
     }

 }

 function KillMeNow(){
 deathClock = deathTime ;
 startDeathClock = true;

 }

Now my problem: How do I know where the instantiated object came from so I can spawn another one at the location where it was first instantiated from.

Right now they are all coming from the same object when they die.

Comment
Add comment
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

2 Replies

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

Answer by · Sep 14, 2010 at 04:38 AM

You could create a variable, kept on the spawned instance, that stores which 'spawner' it belongs to.

On a script attached to the spawned instance (which I will refer to as 'scriptName'):

var spawnOrigin : GameObject;

In SpawnAnother(), when you create the object, you would:

localObject.GetComponent(scriptName).spawnOrigin = gameObject;

This sets the 'spawnOrigin' variable (on the spawned instance) to the GameObject that spawned it.

You would then use the spawnOrigin variable to SpawnAnother(), i.e.:

spawnOrigin.GetComponent(SpawnTarget).SpawnAnother();

Without your full code, I can't post a complete snippet, but hopefully you'll be able to piece it together. Any questions, please ask.

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 EndBoss · Sep 14, 2010 at 05:17 AM 0
Share

Thanks I will try this out. I'm also trying it by giving my spawners a unique name and na$$anonymous$$g the instantiated objects.

thanks!

avatar image · Sep 14, 2010 at 05:21 AM 0
Share

Giving them all unique names is sure to work, but less scalable. With this approach, you should be able to add as many spawners as you want, and it will programmatically create/maintain the links. Best of luck!

avatar image
1
Best Answer

Answer by Herman-Tulleken · Sep 14, 2010 at 04:34 AM

The easiest would be to store the position (and rotation) of the object in the Start function, and use it to spawn the object:

(I am not a native JavaScript speaker, so the syntax might be a bit off). The following must be in the same script as the function SpawnAnother in your code.

var initialPosition : Vector3; var initialRotation : Quaternion;

function Start() { initialPosition = transform.position; initialRotation = transform.rotation; }

function SpawnAnother() { var localObject : GameObject = Instantiate(gameObject, initialPosition, initialRotation); localObject.GetComponent(moveTarget).speedLeft = myCounter + 2; localObject.GetComponent(moveTarget).speedRight = myCounter + (-2); }

(P.S. I am not sure why you had gameObject as parameter for instantiate - I am assuming here that you want to clone the attached object, so I replaced it with gameObject).

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 · Sep 14, 2010 at 04:40 AM 0
Share

I think you've misinterpreted what z3lda is asking. As I read it, the SpawnAnother() object does not move. It initially spawns an object, and when the object is killed, it is meant to spawn another from the spawner it originated from.

avatar image Herman-Tulleken · Sep 14, 2010 at 05:34 AM 0
Share

Yes, I am totally confused... Luckily, you and z3lda understand each other, so all is good :-)

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

No one has followed this question yet.

Related Questions

Parenting Instantiated objects on Collision 2 Answers

Set parent of instantiated object. 0 Answers

Wont let me instantiate gameobject when i destroy it? 0 Answers

Instantiate a Prefab as child 0 Answers

Setting parent of instantiated sprite 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