Change color of specific object

Hi, I am trying to change the color of specific game object, which is rightLeg. So I added Mesh Renderer even though I have seperate object for skinned Mesh Renderer. Also I tried to change color by chaning vertex color, but it still not working. I am using Toon/Basic outlined shader. It gives me error which is “NullReferenceException: Object reference not set to an instance of an object.” Can you guys please help me out here? I spent more than 3days just for this issue and I am almost frustrated with unity. Here is my code as follows :

var armValue: float;
var legValue: float;
var leftArm:GameObject;
var rightArm:GameObject;
var leftLeg:GameObject;
var rightLeg:GameObject;

 function Start () {
var mesh:Mesh  = GetComponent(MeshFilter).mesh;
var vertices : Vector3[]=mesh.vertices;
var colors:Color[] = new Color[vertices.Length];
for(var i= 0;i<vertices.Length;i++)
colors[i] = Color.Lerp(Color.red,Color.green,vertices[i].y);
mesh.colors=colors;
 
}
function Update () {

rightLeg.GetComponent(SkinnedMeshRenderer).renderer.material.color=Color.red;
rightLeg.renderer.material.color=Color.red;
UpdateHoldDrag();
}

Please use code tags. http://forum.unity3d.com/threads/143875-Using-code-tags-properly
Can you post the full error details? I want to know the line that the error is pointing to.

Thanx for advice! This is my first post and the exact error is on line 19.

NullReferenceException: Object reference not set to an instance of an object
Arm.Update () (at Assets/Scripts/Arm.js:19)

Did you pass in the game object for rightLeg in the inspector?

Yes, Also I tried without line 19, but it shows another error which is

MissingComponentException: There is no ‘Renderer’ attached to the “RightUpLeg” game object, but a script is trying to access it.
You probably need to add a Renderer to the game object “RightUpLeg”. Or your script needs to check if the component is attached before using it.
Arm.Update () (at Assets/Scripts/Arm.js:20)

So where is the skinned mesh renderer for your rightLeg object? The null exception is probably because your rightLeg object does not have a SkinnedMeshRenderer component (hence, returning null because you tried to access it).

So, the skinned mesh renderer is located under the main game object, which I imported from the asset store. The rightLeg object is the bone structure which contains transform attribute. The script is referenced to the main object, so rightLeg object is not directly pointing SkinnedMeshRenderer as you said. How can I connect between

You can only get the SkinnedMeshRenderer via the rightLeg object if:

  1. The object with the SkinnedMeshRenderer is either the parent or child (You can reference the parent or child to get the component) OR
  2. You pass in the object with the SkinnedMeshRenderer by creating a public variable and passing it via the inspector. OR
  3. You do a GameObject.Find to find the object with the SkinnedMeshRenderer component (I don’t recommend this since it could get expensive)

It’s your choice really. I would say just create a public SkinnedMeshRenderer variable and pass it in via the inspector.

1 Like