I have created an empty game object and a cube in the Scene.
Created two variables in the script as shown below and already assigned them in the inspector.
Created my own Function to set Material As shown below.
I have attached this script to the empty game object.
but i am getting an error : "Assets/NewBehaviourScript.js(8,14): BCE0019: ‘SetMaterial’ is not a member of ‘UnityEngine.GameObject’. "
here is the script:
#pragma strict
var Black : Material; // i have created a Material with black color and added it into this variable in the inspector
var Cube : GameObject; // i have created a Cube and added it into this variable in the inspector
function Start ()
{
Cube.SetMaterial(Black);
}
function Update ()
{
}
function SetMaterial (materialRef : Material)
{
GetComponent.().material = materialRef;
}
You can’t just call a custom method directly on a GameObject (and I suspect you don’t have any custom components on your cube anyway).
Your GetComponent will operate on the GameObject that you’ve attached your script to, NOT the cube. By itself, it’s equivalent to this.GetComponent.
What you’re really trying to do is get to the Renderer of your cube. I do C#, so I don’t know if this is valid JavaScript (consider switching to C# by the way!).
var cubeRenderer : Renderer;
function Start()
{
cubeRenderer = Cube.GetComponent<Renderer>();
}
Pass cubeRenderer into your SetMaterial and do renderer.material = materialRef, or just do it inline in Start like cubeRenderer.material = Black;
I cant use “renderer.material = materialRef”
every time i use it i get a window popup message saying :
API Update Required and the system will update the script, and if i say no and leave it i get an error :
Assets/NewBehaviourScript.js(19,18): BCE0019: ‘material’ is not a member of ‘UnityEngine.Component’.
You’d have to define “renderer” in your function parameters. (a better name might be myRenderer so it doesn’t get confused with the GameObject you’re on)