Hi. I want to instantiare a gameobject, then add force to its child. How can I do that?
var rg = Instantiate(RagL, this.gameObject.transform.position, this.gameObject.transform.rotation);
childingameobjectiinstantiated.AddForce(Vector3.up * 1000);
Answer by Pawscal
Nov 21, 2014 at 08:09 PM
from looking at your code,it seems the game object you instantiate is never parented to any GameObject. And if your going to destroy the parent, you dont need to parent it anyway i gess.
it should look something like this:
var rg = Instantiate(RagL, transform.position, transform.rotation);
rg.rigidbody.AddForce(Vector3.up * 1000,ForceMode.Impulse); //or whatever ForceMode you need to achieve the effect your looking for
Also, you dont need all these references to "this.gameobject" everywhere. if you want to parent the newly instanciated object, you could add rg.transform.parent = transform; But i'm pretty sure if you destroy the parent game object, the child will be destroyed too.
I did not tested the script and since i dont really know JS there might be an error or two in synthax, but i think it should work.
Hope it helps.
Im instantiating an object with few children. By writing rg.addforce, im adding force to the parent. Im looking for a way to add force to its child.
Maybe I could use some other method. Is there any way to add force to objects top or bottom, because addforce always applies force to objects center
Hey dude, 3 years later and i wanna do the same thing.
The thing is, I am instantiating an GameObject with 5 children that have Rigidbodys. What i wanna do is acces each Rigidbody component and add force to it. I was thinking about a foreach loop but im not that skilled. Have you figured it out?
how about this? if (transform.GetChildCount > 0) for (int i = 0; i < transform.GetChildCount; i++) transform.GetChild(i).rigidbody.AddForce(myForceVector);
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
5 People are following this question.
Checking if object intersects?
Raycast not working with instantiated objects.
Put PhysicMaterial on my Gameobject befor destroying it?
Instantiate new object from scratch