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 bridged · Jul 24, 2012 at 03:29 PM · gameobjectinstantiateprefabdestroy

Accessing children of an instance doesn't work every time

Hey,

I don't get the problem with the following code:

 using UnityEngine;
 using System.Collections;
 
 public class brain1 : MonoBehaviour {
  
  public GameObject level;
  public GameObject cube;
  
  public int i = 1;
  
  // Use this for initialization
  void Start () {
  
  }
  
  // Update is called once per frame
  void Update () {
  
  if(Input.GetKeyDown (KeyCode.U))
  {
  if(level)
  Destroy(level);
  
  level = Instantiate (Resources.Load ("container"+i)) as GameObject;
  i++;
  
  cube = GameObject.Find("army");
  }
  
  Debug.Log (cube.transform.childCount);
  }
 }

container1 is a prefab (empty gameobject) in "Resources" with an empty gameobject in it which is called "army" and has 1 cube in it.

container2 is the same thing with 2 cubes in "army". container3 still the same with 3 cubes in "army".

now I want get the number of children from the Debug function when I press U.

When I press it the first time it works and I get the msg "1".

When I press it the scond time I get the error:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you hould not destroy the object. brain1.Update () (at Assets/Standard Assets/Scripts Test/brain1.cs:30)

When I press it the third time, I get "3"

wtf?

So that means, that someone there is no gameobject level. But it appears on the scene!

How can I access the gameobject level correctly?

I tried a lot of stuff and did a lot of research today and the last day for hours but I can't get what is wrong. Please help!

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

1 Reply

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

Answer by Kryptos · Jul 24, 2012 at 05:18 PM

Destroy does not destroy immediately but at the end of the frame. So it is possible that GameObject.Find finds the destroyed cube because it is still there.

The best solution would be to avoid GameObject.Find, and to iterate through the children of the new instantiated prefab to get the cube.

Something like:

 level = Instantiate (Resources.Load ("container"+i)) as GameObject;
 i++;

 foreach(Transform child in level.transform)
 {
     if(child.gameObject.name == "army")
     {
         cube = child.gameObject;
         break;
     }
 }

Anyway, after you call Destroy it is safer to set all references to null:

 if(level)
     Destroy(level);
 level = null;
 cube = null;
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 bridged · Jul 26, 2012 at 05:37 PM 0
Share

"Destroy does not destroy immediately but at the end of the frame."

that is absolutely the information that I need to solve the problem and another too, thx alot!

avatar image Kryptos · Jul 27, 2012 at 07:38 AM 0
Share

To be more precise:

Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.

I'm guessing this happens between LateUpdate and OnWillRenderObject

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer

Trouble with destroying an instantiated prefab 2 Answers

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

Can't remove instantiated prefab 0 Answers

Issues with destroying a game object 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