• 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
Question by Makiavel · Nov 02, 2012 at 01:01 PM · javascriptgameobjectactivedeactivate

Referencing children of an instantiated object.

 var Possibility1:GameObject;
 var Possibility2:GameObject;
 var Possibility3:GameObject;
 var Possibility4:GameObject;
 var Possibility5:GameObject;
 var Choice : GameObject;
 var AppearDist:int = 50;
 var x:int;
 var Player : Transform;
 var Real_Object : GameObject;
 var a : boolean = true;
 
 
 function Start ()
 {
 Player = GameObject.FindWithTag("Player").transform;
 }
 
 function Update () 
 {
 
 if(a == true)
 {
 x = Random.Range(1.0, 5.0);
 if(x==1)
 {Choice = Possibility1;}
 if(x==2)
 {Choice = Possibility2;}
 if(x==3)
 {Choice = Possibility3;}
 if(x==4)
 {Choice = Possibility4;}
 if(x==5)
 {Choice = Possibility5;}
 Real_Object = Instantiate(Choice, transform.position, Quaternion.identity);
 a=false;
 }
 
 var dist = Vector3.Distance(Player.position, transform.position);
 
 if(dist>AppearDist)
 {
 Real_Object.active = false;
 }
 else
 {
 Real_Object.active = true;
 }
 
 }



This script is supposed to create one of five objects at the place of an object it is attached to, and then calculate the distance between itself and the player. If the distance is big enough - disable the instantiated object. And then enable it again, when player gets close.

Problem is, it deactivates only the parent object, but not its children. I used the

 for (var child : Transform in transform) 
 {
     child.renderer.enabled = false;
     renderer.enabled = false;
 }

for previous version of a script, attached to the instantiated object itself (back then I just made it invisible). Is it possible to reference the children of the instantiated object in this case?

Problem is, I can not search by Tag or something like that, because there is a whole grid of these with a random width, length and hight, so referencing should be local.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by valyard · Nov 02, 2012 at 01:36 PM

You should use GameObject.SetActiveRecursively.

Also deactivating a GameObject and disabling its renderer is not the same.

Comment

People who like this

0 Show 1 · 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 Makiavel · Nov 02, 2012 at 01:58 PM 0
Share

About the second part - I know) I'm not that hopeless...) I just posted it as an example of what I meant. Thank you for the answer!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Restart this script 3 Answers

Trouble getting GUI phone to open/close 1 Answer

Deactivate an object - and all scripts in that object deactivated? 1 Answer

Randomly deactivate game objects 2 Answers

Turning off rigidbody without turning the entire object inactive? 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