- Home /
How to acsess the renderer of an Instantiate()
I need to acsess the renderer of an object that I Instatiate() and It keeps saying that "renderer" is not a member of "Object".`enter code here`
See the code below for the script. Thanks!
#pragma strict
var xpos : float;
var spawnpoint : Vector3;
var coin : GameObject;
var rotation : Quaternion;
rotation.eulerAngles = new Vector3(0, 0, 0);
var newCoin :Object;
function Start () {
InvokeRepeating("CreateCoin", 1, 1);
}
function Update () {
Destroy(GameObject.FindWithTag("Coin"),10);
}
function CreateCoin(){
xpos = Random.Range(-50.0,50.0);
spawnpoint = new Vector3(xpos, 80, 0);
var myColor : Color; // This is just working for another part of my script, Please Ignore.
var mode : boolean = Random.value > 0.5f;
var rand : float = Random.value;
var index : int = Random.Range( 0, 3 );
myColor[index] = rand;
myColor[( index + ( mode ? 1 : 2 ) ) % 3] = 1;
myColor[( index + ( mode ? 2 : 1 ) ) % 3] = 0;
newCoin = Instantiate(coin, spawnpoint, rotation);
//newCoin = newCoin as GameObject;
newCoin.renderer.material.color = myColor;
}
Answer by pdunton · Feb 06, 2014 at 05:04 AM
Hey, for some reason I cannot comment.... So this is a comment. Ill try to look up how to fix it but it just keeps saying 10 Characters needed even though I type >10.
But I've tried that and It says that I forgot to put in a semicolon at the end of that line. Here's what it reads now...
newCoin = (GameObject)Instantiate(coin, spawnpoint, rotation);
You should also change
var newCoin :Object;
to
var newCoin : GameObject;
Apperently my commenting fixed? Anyways, I changed the type to GameObject as you suggested, and the same is happening, I keep getting the "Assets/Resources/Scripts/CoinCreater.js(29,31): UCE0001: ';' expected. Insert a semicolon at the end. " error. :(
Uhhhhh... I tried a couple of things, and it turns out, the correct thing to do is to have
var newCoin : GameObject;
but not have
newCoin = (GameObject)Instantiate(coin, spawnpoint, rotation);
Wierd, huh? Anyway, thanks a ton!
Answer by zee_ola05 · Feb 06, 2014 at 04:39 AM
Instantiate returns an Object class, not a GameObject. So you simply cast.
newCoin = (GameObject)Instantiate(coin, spawnpoint, rotation);