hello how can I fix the NullReferenceException: Object reference not set to an instance of an object, the error is in this line of code
touchCurrentRenderer.material.color = touchOriginalColor;
And below is my code
public GameObject SnapLocation;
public GameObject HoldObject;
public GameObject HighLightObject;
public bool isSnapped;
private bool objectSnapped;
private bool grabbed;
private Renderer touchCurrentRenderer;
private Color touchOriginalColor;
public Color touchHighlightColor;
public Renderer rend;
private void Start()
{
}
void Update()
{
grabbed = this.GetComponent().isGrabbed;
objectSnapped = SnapLocation.GetComponent().Snapped;
Renderer rend = HighLightObject.GetComponent();
rend.enabled = false;
if (grabbed == true)
{
// Get Renderer component
touchCurrentRenderer = HighLightObject.GetComponent();
touchOriginalColor = touchCurrentRenderer.material.color;
// Highlight Object
touchCurrentRenderer.material.color = touchHighlightColor;
rend.enabled = true;
}
if (grabbed == false)
{
touchCurrentRenderer.material.color = touchOriginalColor;
rend.enabled = false;
}
if (objectSnapped == true)
{
GetComponent().isKinematic = true;
transform.SetParent(HoldObject.transform);
isSnapped = true;
}
if (objectSnapped == false && grabbed == false)
{
GetComponent().isKinematic = false;
}
}
Which line of code is giving error ?
It appears to be a missing component exception
this one
touchCurrentRenderer.material.color = touchOriginalColor;
What is "touchOriginalColor" ?
It looks like you havenât defined this variable yet.
as I understand I did it in this line or not?
touchOriginalColor = touchCurrentRenderer.material.color;
touchOriginalColor = touchCurrentRenderer.material.color;
touchCurrentRenderer.material.color = touchHighlightColor;
This isnât going to work - what do you have as the value of touchCurrentRenderer.material.color in Unity?
This is the most basic way of defining a colour in Unity:
Color newColor = new Color(0.3f, 0.4f, 0.6f, 0.3f);
Okay so at first I should define touchOriginalColor like this,
Color touchOriginalColor = new Color(0.3f, 0.4f, 0.6f, 0.3f);
and then do this code like this
touchOriginalColor = touchCurrentRenderer.material.color;
Am sorry I donât have that experience in coding
No itâs because touchCurrentRenderer.material.color doesnât have a value.
Itâs like saying âWhat is something?â and then answering âitâs thatâ - the person still doesnât know what it is.
Color touchOriginalColor = new Color(0.3f, 0.4f, 0.6f, 0.3f);
touchCurrentRenderer.material.color = touchOriginalColor;
You can define all these and GetComponent stuff outside of Update so itâs not looping over the lot, but itâs probably not important here.
1 Like
davidnibi:
No itâs because touchCurrentRenderer.material.color doesnât have a value.
Itâs like saying âWhat is something?â and then answering âitâs thatâ - the person still doesnât know what it is.
Color touchOriginalColor = new Color(0.3f, 0.4f, 0.6f, 0.3f);
touchCurrentRenderer.material.color = touchOriginalColor;
You can define all these and GetComponent stuff outside of Update so itâs not looping over the lot, but itâs probably not important here.
Ya because it still has the same problem when I tried it; a Null reference exception
Yes it has a color property