Duplicate Question. Try googling around and the answer should be simple
Help with turning a random gen # into text mesh.
I keep getting an error "NullReferenceException: Object reference not set to an instance of an object RandomGen1.RandGen () (at Assets/RandomGen1.cs:13) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()" I think I know why but I'm not completely sure the code is
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
public class RandomGen1 : MonoBehaviour
{
public GameObject TextBox;
public int RandomNum;
public void RandGen() {
RandomNum = Random.Range(1, 1000);
TextBox.GetComponent<Text>().text = "" + RandomNum;
}
}
I think it has to do with because it is trying to find a text box but I don't have one since i'm using the text mesh boxes but I'm not 100% sure.
Hi who ever you are.
This is a really low effort question, I recommend you read how to post questions - see FAQ on the right side... :
"Badly formated question: code needs to be formatted using the 101/010 button, break your post up into several parts so it's understandable what you are asking, and make sure there is a question in your post"
"Posting about a specific compiling error or NullReferenceException: there is a myriad of these questions with answers already, have a look at those posts to get hints on what could possibly be your issue"
Answer by tormentoarmagedoom · Sep 06, 2018 at 03:43 PM
Post removed.
Follow the rules. This error can be solved with 5 minutes at google