instantiated prefab to the child of gameobject

hi all,

i'm getting trouble to make prefabs as a child of the gameobjects.... fist my game status is, i'm having 5 holes, and i want to put 4 stones in each hole..so , ihave created a holes and stored in an array,and make stone prefabs comfortable and now i want to make each 4 stones as a child of hole gameobject....

here my code:

void Clone()
    {

    int j=0;
    int i = 0;
    for(j=0; j<5; j++)
    {
    for(i=0; i<4; i++)
    {
    clone = Instantiate(stone,hole[j].transform.position,transform.rotation) as GameObject;
    }

    }
clone.transform.parent = hole[j].transform;
}

am getting error as "object reference not set to instance of object"....help pls?

thank you.

I think you need to put the clone.transform.parent = hole[j].transform; inside the loops. Try:

void Clone()
    {

    int j=0;
    int i = 0;
    for(j=0; j<5; j++)
    {
    for(i=0; i<4; i++)
    {
    clone = Instantiate(stone,hole[j].transform.position,transform.rotation) as GameObject;
    clone.transform.parent = hole[j].transform;    
    }

    }

}

This works for me: Use the Editor to add 5 GameObjects to the hole array and the stone to instantiate.

public class Test : MonoBehaviour {

    public GameObject stone;
    public GameObject[] hole = new GameObject[5];

    void Start () {
        GameObject clone;
        int j = 0;
        int i = 0;
        for (j = 0; j < 5; j++)
        {
            for (i = 0; i < 4; i++)
            {
                clone = Instantiate(stone, hole[j].transform.position, transform.rotation) as GameObject;
                clone.transform.parent = hole[j].transform;
            }

        }
    }
}

Edit: After reviewing the code (see comments) i found that the error occurs because the "stone" variable is defined as a Transform object but after it is copied using the Instantiate function the result is typecasted as a GameObject (this is not possible and returns null). The call to clone.transform.parent gives an error because clone is null.

Solution is simple: Define stone as a GameObject instead of a Transform object

public GameObject stone;

You might need to set the stone variable one more time in the Editor to be sure.

Hope this helps

Either your clone object or stone object has not been instantiated.

In other words , putting ILikeCats! there would be just as usefull since it's just plain nothing.

Instantiation should be done using the new keyword or a read operation such as GetComponent.

My gut feeling says clone is not the problem since then you would receive another compiler error since that's just a write operation. If that Instantiate fails however and it stays null you will get the error you just did when trying to acces a property of clone with the dot operator.

help in same problem. But using .js

how to inst my object under parent?

var torchPrefab : Transform;

function Update(){ if(Input.GetButtonDown("Fire2")){ var torch =Instantiate (torchPrefab,GameObject.FindWithTag("wrench").transform.position,transform.rotation);

}

}