- Home /
Object reference not set to an instance of an object FirstSelected
Image of the error: http://gyazo.com/af458f1f129718b55f2f56395676fcd7
script:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class FirstSelected : MonoBehaviour {
EventSystem eventSystem;
//public GameObject selected;
public GameObject gameObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
eventSystem.SetSelectedGameObject(gameObject);
}
}
I believe this allows me to have certain things be selected when the UI pops up. Doesn't seem to be working correctly not really sure what I'm doing wrong
Answer by siaran · Apr 30, 2015 at 04:01 PM
You just have an empty EventSystem variable, it's not actually set to anything. So it's null. You'd have to do something like eventSystem = GameObject.FindObjectOfType()
in your Start() function.
(Actually, try not to use GameObject.Find, maybe make your eventSystem variable public and drag the object to it in the editor? Best way depends on your specific needs of course).
also, you probably don't want to call SetSelectedGameObject each frame, but only when the selected gameobject changes? Just a thought.
so in start something like eventSystem = gameObject ??
if so then I get an error saying it cant convert a gameobject to eventsystems hmmmm
Your answer
![](https://koobas.hobune.stream/wayback/20220613194438im_/https://answers.unity.com/themes/thub/images/avi.jpg)