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 cj31387 · Sep 22, 2012 at 01:24 AM · collisiontriggerchildsocket

Scripting Child Objects Through Parent

Can someone give me a few C# code examples on the ways to interact with child objects. I don't know how to select specific ones through script, that is mainly what I want to know. Basically I have a spawn socket prefab of child objects and I need to know how to script to children objects, through the parent.

I have a prefab with children game objects laid out in space. It is a formation. I want my instantiated fighters and Dropships to find an empty slot on one of the prefabs spawn sockets (child) then go hover over it and it then say that it is full and the next fighters go to the next free slot till the formation is full. when its full a new formation will randomly spawn in another place, and the same thing will happen. Eventually they will move towards the player and attack.

Ok my null reference error is solved by another question. Basically I want the mothership to spawn the enemies, then have them look for a empty slot and physically move into position, not just instantly spawn into it. Because once one formation is full another will be created and more fighters will fill up the next formation and so on. But what I'm having problems with is I'm using a bool in my formation script to check if a slot is full or not, another script to check for a collision on one of the slots, if there is a collision Formation_AI.isEmptySlot = false; It should be false and the fighters go to the next slot with the other ifs, but its not turning false for some reason, I checked with debug and its staying true in the formation_AI, but I put debug in the collision script and it says false in that script but its not changing the formation script to false.

Comment
Add comment · Show 4
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 AlucardJay · Sep 24, 2012 at 06:14 AM 1
Share

I am not sure what anyone can offer you except Transform.Find and Component.GetComponent :

Finds a child by name and returns it : http://docs.unity3d.com/Documentation/ScriptReference/Transform.Find.html

Returns the component of Type type if the game object has one attached, null if it doesn't : http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html

avatar image cj31387 · Sep 24, 2012 at 06:17 AM 0
Share

Thanks alucardj, I've looked at transform.find before but this looks different. I didn't think of assigning it as a variable. This will probably work assign each child as a variable then use the variable to do stuff.

avatar image AlucardJay · Sep 24, 2012 at 06:20 AM 2
Share

Also when you instantiate the object, make it a child of the scripts' object. You could also rename it for ease when searching for objects by name. Here is an example :

 Transform cloneCube = Instantiate( myCube, Vector3.zero, Quaternion.identity );
 cloneCube.parent = this.transform;
 cloneCube.name = "$$anonymous$$yCube_" + someString;
 

This is from one of my scripts that creates and instantiates an array of cubes. I have done similar where an object is instantiated, then GetComponent to find the script attached to the instantiated prefab (or attaching a script to that instance), then storing a reference to that script in an array. Is this what you were after?

To clarify : Are you looking to Instantiate an object, attach a script to that object, then store that object in a variable or array ?

avatar image AlucardJay · Sep 24, 2012 at 06:44 AM 1
Share
  • find how many spawn objects are in the prefab , let's call it allSpawnPoints

  • create an array of Transform , the length of allSpawnPoints

  • loop through all the items in allSpawnPoints

  • find the next position in the formation, store a reference Vector3

  • Instantiate a Fighter Ship into the Transform array, assign its transform.position the stored Vector3.

  • after the loop has finished, you have instantiated the ships in all the positions, and have a reference to all of them stored in one array.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 27, 2012 at 05:29 AM

You can get any child directly with the function Transform.GetChild(zeroBasedIndex), or find it by name with Transform.Find(objectName):

 var child1: Transform = transform.GetChild(1);
 

or

 var child1: Transform = transform.Find("Child1");
 

GetChild was removed from the docs due to some unexplained reason, but it's active and is used internally by Unity. Unfortunately, using it may be tricky when children are inserted or deleted, because the indexes of other children may be changed. Find(name) is slower, but doesn't have this index issue (provided that you give a different name to each child, of course).
Another alternative (although not suitable in this case, I presume) is to use foreach (C#) or for..in (JS) to iterate through all children. This is fast and simple, but you don't get the child index - just its Transform:

 var n: int = 0;
 for (var child: Transform in transform){
   // assign a sequential number to the variable childNum
   // in the script SomeScript in each children:
   child.GetComponent(SomeScript).childNum = ++n;
 }
Comment
Add comment · 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

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

12 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

Related Questions

OnCollision events inside child with collider 0 Answers

C# Ignore Collision Trigger From Child 2 Answers

Player object still gets destroyed even when shields up 1 Answer

Getting a Trigger's parent 1 Answer

Detecting collision on an object and not its sister object. 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