Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 binoculars88 · Dec 04, 2017 at 08:33 PM · instantiatedestroy

Reinstantiated object seems destroyed

At the begin of my script, I do:

 void InstantiateTargets(int amount)
 {
     var i = 0;
     while (i <= amount-1) {
         Instantiate (targetPrefab, new Vector3 (0, -100, 0), Quaternion.identity).name = "target"+i;
         i++;
     }
 }

and

 void AssignTargets()
 {
     targets = GameObject.FindGameObjectsWithTag("target");
     Debug.Log(targets[0]); // comment 1
 }

To illustrate my problem, I put this in the Update void:

 void Update()
 {
      Debug.Log(targets[0]); // comment 2
 }

Later on, I destroy all the targets, and AFTER that, I call both voids again. The weird thing is that comment 1 keeps working: it gives me the correct target. But the moment I destroy the targets, comment 2 gives me null, even after I called the voids again to instantiate them again. The targets exists in the hierarchy, but I can't use them using targets.
Any thoughts what's going on? What am I doing wrong here? Thanks a bunch!

Comment
Add comment · Show 2
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 ShadyProductions · Dec 04, 2017 at 08:35 PM 0
Share

Not sure what's happening but, you could add these items to a list and delete them when you destroy them in the same class ins$$anonymous$$d of using GameObject.FindGameObjectsWithTag Then you'll always retain the correct reference.

avatar image binoculars88 ShadyProductions · Dec 04, 2017 at 08:50 PM 0
Share

Any chance you could point me in the right direction how to do this?

1 Reply

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

Answer by ShadyProductions · Dec 04, 2017 at 09:08 PM

Perhaps something like this:

 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 namespace Assets.Scripts
 {
     public class DataHandler : MonoBehaviour
     {
         [SerializeField]
         private GameObject targetPrefab;
 
         public List<GameObject> Targets = new List<GameObject>();
 
         /// <summary>
         /// Create new targets.
         /// </summary>
         /// <param name="amount"></param>
         public void InstantiateTargets(int amount)
         {
             for (int i=0; i < amount; i++)
             {
                 var obj = Instantiate(targetPrefab, new Vector3(0, -100, 0), Quaternion.identity);
                 obj.name = "target" + i;
 
                 // Add target to list
                 Targets.Add(obj);
             }
         }
 
         /// <summary>
         /// Destroy one target.
         /// </summary>
         /// <param name="target"></param>
         public void DestroyTarget(GameObject target)
         {
             var instanceId = target.GetInstanceID();
             target = Targets.FirstOrDefault(f => f.GetInstanceID() == instanceId);
             if (target != null)
             {
                 Targets.Remove(target);
                 Destroy(target);
             }
         }
 
         /// <summary>
         /// Destroy multiple targets.
         /// </summary>
         /// <param name="targets"></param>
         public void DestroyTargets(IEnumerable<GameObject> targets)
         {
             foreach (var target in targets)
             {
                 // Get unique gameobject id
                 var instanceId = target.GetInstanceID();
                 var targetHolder = Targets.FirstOrDefault(f => f.GetInstanceID() == instanceId);
                 if (targetHolder != null)
                 {
                     Targets.Remove(targetHolder);
                     Destroy(targetHolder);
                 }
             }
         }
     }
 }
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 binoculars88 · Dec 04, 2017 at 09:23 PM 0
Share

Thanks, I'll try this. I'm new to Unity so this is a very basic question, but how can I change the position of 1 specific List item? I used to do targets[i].transform.position = ... with the scripts of my original question. Can I use Targets[i] or something?

avatar image ShadyProductions binoculars88 · Dec 04, 2017 at 09:24 PM 0
Share

Same way

 Targets[index].transform.position

On a side note, make sure to call the DestroyTarget methods and not destroy them like Destroy(Targets[i]); this will leave fake null objects in the List.

avatar image binoculars88 ShadyProductions · Dec 04, 2017 at 10:13 PM 0
Share

Thanks a bunch, it works! Not that relevant, but I've managed to make my old script work too. Is there a performance or other benefit when using List ins$$anonymous$$d of Arrays like I did?

Show more comments

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

93 People are following this question.

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

Related Questions

Proper/Best way to Instantiate and Destroy with Unity Networking? 2 Answers

instantiate,destroy,gain speed in time 2 Answers

Duplication(instance) re-create deleted children 0 Answers

Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer

Can't seem to destroy instantiated objects. 1 Answer


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