Question by
nammidd · Oct 23, 2016 at 06:56 AM ·
instantiatecolor
change color of instantiated object
I badly know C# and OOP, how to change color of instantiated object? Could you tell me? This way I can not do it:
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour {
public GameObject Сube;
void Start () {
Instantiate(Сube, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity);
Cube.GetComponent<Renderer>().material.color = Color.blue;
}
}
Comment
Answer by OmniKeith · Oct 23, 2016 at 07:05 AM
you need to apply a material to the new instantiated object first before you change it's color. the simplest thing to do is make a public variable material and assign it in the inspector. then you can change it all you want.
But why then i can change color of object on which script hangs without a material? Same way - gameObject.GetComponent().material.color = Color.green;