using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TouchController : MonoBehaviour
{
public TextMesh Check;
public Graphic left;
public Graphic right;
Color rc;
Color lc;
public void Start()
{
left = GetComponent<Graphic>();
right = GetComponent<Graphic>();
rc = Color.clear;
lc = Color.clear;
}
public void LeftClick()
{
Check.text = "left";
left.color = lc;
right.color = rc;
}
public void RightClick()
{
Check.text = "Right";
left.color = lc;
right.color = rc;
}
}
Everytime RightClick() or LeftClick() is run i get a NullReferenceException on the line left.color = lc; anyone know why?
p.s I have set the graphics and textmesh in the inspector.
public TextMesh Check;
public Image left;
public Image right;
Color rc;
Color lc;
public void Start()
{
left = GetComponent<Image>();
right = GetComponent<Image>();
rc = Color.clear;
lc = Color.clear;
}
public void LeftClick()
{
Check.text = "left";
left.color = lc;
right.color = rc;
}
public void RightClick()
{
Check.text = "Right";
left.color = lc;
right.color = rc;
}
something like this, if the error stays then check if the script still have the link to the image at the click-time. Start the game, then the game will be paused by the getting error. Switch to the scene tab with running game and check Image fields.
Ok so, i did that and the error stayed, the game still ran despite the error and I found that when the game was being run the link to the Image was lost. Also there was no change with any of the image fields
left = GetComponent();
right = GetComponent();
there is no need for both lines. You have already this link from the
public Image left;
public Image right;
And the line with getcomponent overwrites this link.