Copying and cloning objects

I’m playing with Instantiate method in C#, but it does not work as expected.
I want to copy (clone) object and play with it’s properties later. Here is what i do:

public GameObject cube;	
void Start () 	
{
	GameObject c;
	c = Instantiate(cube, new Vector3(0,0,0), Quaternion.identity) as GameObject;
}

With this i get another object on stage, but it’s not stored in c. I get warning: The variable `c’ is assigned but its value is never used
if i try to access any of c properties, like translate, rotation or whatever i get further errors.

The scope of the variable c is ended just after you initialize it (as it’s declared in the Start method). To store the gameobject use your variable cube or some other variable which is persistent.

If i store it in cube variable i do not get warnings ( or declare c outside of Start ), but i still can’t access to that object.
I’m trying to access to transform properties of copied object, but i get “Object reference not set to an instance of an object” error.

Well, that’s odd…
Maybe try to cast with parenthesis ?

This works for me :

public GameObject m_oCube;

void AnyFunction()
{
Vector3 pos = new Vector3(x, y, z); //to set
GameObject cube = (GameObject)Instantiate(m_oCube, pos, Quaternion.identity);
cube.transform.parent = transform; // Can be anything else
}

maybe u just tryin to access it’s properties before the variable has been initialized (before Start called)?
or maybe cube is null… hard to say with such a small code snippet

Hmh…
If i use Transform instead GameObject everything is ok…