Hi,
I’m pretty new to Unity , but not to programing. I’ve never seen such a weird problem: Everything i do or set is ALLWAYS null.
Basically what i want to do is to make a method that whenever it gets called it will increase the emission value of the selected object’s material to 5 (im making car lights).
This is my code:
using UnityEngine;
using System.Collections;
public class VehicleLightsManager : MonoBehaviour {
public GameObject carLights;
Color finalValue = Color.red * 5;
public void BrakeLightsOn ()
{
carLights.GetComponent().material.SetColor(“_EmissionColor”, finalValue);
}
}
The problem is that i get null exception to the GetComponent method (or any other method, i’ve tried this in many ways because i thought the exception is coming from a wrong code) and even to the GameObject i set in the inspector (!).
When i put the code in the Start method everything is OK, what am i missing??
Hi Daniel. This should ideally be posted in the scripting forum.
Are you specifying the gameobject carLights in the editor (dragging the game object onto the script) or are you attaching this script to the gameobject that is carLights? If it’s the latter, you could do this:
Hi Pete, thank you for your quick answer and sorry for not posting this thread in the right section, I’m new to this forum.
this script is attached to the car itself, and i’ve draged the light gameobject to it (a game object that is part of the car).
At first I have assumed it will be better to attach the script to the object itself but the exception still poped. I prefer using this script as a manger to all lights (instead of attaching it to the light itself) because it will make things much easier when I’m editing a complex car with many lights.
In that instance, your script should work. You need to make sure that the object you’re dragging into your light controller has the Mesh Renderer component on it that you are trying to step the emissive value of.
I know it should work, but it does’nt. Usually it isn’t that difficult to fix this kind of problems, this time i really have no idea what’s wrong.
My object does have a Mesh Renderer…
When do you call this method? Are you sure the object is already or still linked/existing when you call this method?
Add this to your script to see what really is missing. Enable “Error Pause” in Unity console and take a look what really is assigned to the carLights field.
if (carLights == null)
Debug.LogError("Object is missing");
if (carLights.GetComponent<Renderer>() == null)
Debug.LogError("Object has no mesh renderer");
Hi EyecyArt,
The first thing that i did was to check if the object still exist (i call this method every time i press the space button, to brake the car) and i did got the messeges in the console. So i’ve created a cube to be the light object (unity default cube), increased its scale so i can clearly see it in play mode, and i still got the messege. The cube is 100% not null.
Is there a way to debug the code? real debug not with if statements…