cannot modify instantiated object

Hi,

I am learning Unity at the moment. I tried to create a slope and create some balls to fall on it and want to see them rolling, however I can’t seems to modify the tile’s transform, here’s my code :

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

public Transform stoneBall;
public Transform tile;

void Start () {

	GameObject newObject = (GameObject)Instantiate (tile);
	newObject.transform.position = new Vector3 (10, 15, 10);
	newObject.transform.Rotate (20f,20f,20f);

}

void Update () {

	int x,y,z;
	x = Random.Range (-10, 10);
	y = Random.Range (1, 10);
	z = Random.Range (-10, 10);
	Instantiate(stoneBall, new Vector3 (x, y, z), Quaternion.identity);

}

}

These two lines doesn’t seems to work, if I modify the Prefab’s transform rotation through inspector, then i can’t see the balls rolling after it land on the tile slope, but I can’t seems to make it work using code.

I feel like I missed something very basic?? Thanks.

I can create the slope by using the following code instead, but not using Instantiate :

	// tile
	GameObject tile2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
	tile2.transform.position = new Vector3(0, -3, 0);
	tile2.transform.localScale += new Vector3(20, -0.1f, 10);
	tile2.transform.Rotate(new Vector3(0, 0, 20));

Regards,
Alex

It is not supposed to work like this.

First change your member variables to

public GameObject stoneBallPrefab;
public GameObject tilePrefab;

Add 2 more member vars:

private Transform stoneBallInstance;
private Transform tileInstance;

Now instantiate the Prefab and assign the transform of the resulting GameObject to the instance var for tile:

GameObject newObject = (GameObject)Instantiate (tilePrefab);
tileInstance = newObject.transform;

So now you have the tileInstance and can reference it wherever you like to assign position, rotation etc.

Next you instantiate the ball - BUT do it in Awake() as well otherwise you will instantiate a ball each frame!

GameObject stoneBallObject = Instantiate(stoneBall, new Vector3 (x, y, z), Quaternion.identity);
stoneBallInstance = stoneBallObject.transform;

Assign a position etc. to the stoneBallInstace (which is the transform). Now you can start thinking about what to do in the Update method (moving the transforms etc.). But start with an empty Update and see whether both transforms have been created at their positions.

Before hitting the Play button don’t forget to assign proper Prefabs to the Prefab variables (please read the Prefab/Instantiate Unity - Manual: Instantiating Prefabs at run time secion in the docs at least once).

clone = Instantiate(stoneBall, new Vector3 (x, y, z), Quaternion.identity);