0

The question might seem a little too weird but I want to add the object "Capsule" to my gameobject using script (see the photo below)

this is the image for the reference of "Capsule" object

This is my code for adding components in my object:

object1 = (GameObject)Resources.Load (name) as GameObject;
        myObject = Instantiate (object1);
        myObject.transform.SetParent (parent.transform);
        myObject.transform.position = new Vector3 (0, 0, 0);
        myObject.transform.localScale = new Vector3 (70f, 70f, 70f);
        myObject.AddComponent<BoxCollider> ();
        myObject.AddComponent<CapsuleCollider> ();
        myObject.AddComponent<MeshRenderer> ();

        myObject.gameObject.SetActive (true);

What could be the possible solution for this? Thank you very much!

10
  • What is your problem here ? Commented Mar 23, 2017 at 17:29
  • I want myObject to add the "Capsule" as a component. Is that possible? Commented Mar 23, 2017 at 17:30
  • It is not possible to add it as a Component. Do you mean as a children? Commented Mar 23, 2017 at 17:31
  • Uhm, yes as a children. Sorry Commented Mar 23, 2017 at 17:33
  • What does not work in your code? Do you have any error? Does SetParent works? We need more information about your issue here Commented Mar 23, 2017 at 17:34

1 Answer 1

3

There seems to be a misunderstanding of what "Adding a GameObject" means so lets break this down. In Unity, a GameObject is a Transform and a collection of "Components". That Transform can be the "parent" or "child" of another Transform. GameObjects and their "Transform Heirarchy" are what's displayed in the "Heirarchy" Unity window. A single GameObject and its "Components" (things that implement MonoBehaviour) are what's displayed in the the "Inspector" Unity window.

By these terms, you cannot "Add a GameObject to another GameObject" however you can "Add a GameObject as a child of another GameObject" by parenting one Transform to another.

It seems like what you're trying to do here is add your "Capsule" as a child of each of your GameObjects. All you need to do in this case is, for each GameObject, instantiate a new instance of the "Capsule" (it could be setup as a prefab) and parent it to your GameObjects:

public class CapsuleAdder : MonoBehaviour {
    private const string capsulePrefabName = "whatever"; // Prefab name goes here

    void Awake() {
        GameObject newCapsule = Instantiate(Resources.Load(capsulePrefabName) as GameObject) as GameObject;
        newCapsule.transform.SetParent(transform); // Sets parent to this
    }
}

As long as you properly set capsulePrefabName, any GameObject with this script attached will spawn a copy of your capsule prefab as a child.

Sign up to request clarification or add additional context in comments.

3 Comments

should my capsule be added in the resources folder?
@Sarah Yes. Resources.Load will only work for assets in a "Resources" folder.
@Sarah I also forgot the call to Instantiate in my first pass. Edited the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.