#Original Question:
Ok before you say, there are loads of topics about this.
I know… there are about 600 on just the NullRefference issue.
And i ofcourse dont wanna add questions that aren’t usefull to the question database.
The thing is. they honestly didn’t work for me. or i have looked exactly at 10 wrong versions?
i want to grow as a unity coder/designer. So i need to understand this.
I also want to turn this question into something quick and useful for others to read once i get this working.
I understand that NULL bassicly means that theres no data to give.
So i need to define where it has to get the info. in my case i think its because my script is inside a object called “Player2”
thats what i think atleast… anyways here is how simple my code is.
They are two seperate scripts each in there own object. one is in a cube named “Player2” the other is in a mesh text object named “New Text”
using UnityEngine;
using System.Collections;
public class Player_Controller : MonoBehaviour {
public float direction;
public string Str_direction;
//public string test_if_works = "working";
void Update () {
direction = Input.GetAxis("Horizontal");
if (direction < 0.0)
Str_direction = "left";
//print("test"+printresult);
if(direction > 0.0)
Str_direction = "right";
//print("test"+printresult);
if(direction == 0.0)
Str_direction = "stationary";
//print("test"+printresult);
}
}
using UnityEngine;
using System.Collections;
public class FloatingTextSimple : MonoBehaviour {
public GameObject Player2;
private Player_Controller Player_Controller_taken;
void Start () {
Player_Controller_taken = Player2.GetComponent<Player_Controller>();
}
void Update () {
//string newdir = transform.Find("player2").GetComponent<Player_Controller>().Str_direction();
GetComponent<TextMesh>().text = Player_Controller_taken.Str_direction;
}
}
Now the code might look a bit odd. But i tried hundreds of things. And i constantly get NULLRefference errors or different types of erros. But the way i have it now seems to give the least errors atleast…
What am i trying to achieve?
I am trying to display the debug text ingame on text mesh.
Why use text mesh?
Because i read somewhere that its easier and less code (only a few lines)
I think im not understanding the fundamentals. And i would like to ask ontop of this question if somebody can teach me a method where i can learn this easier myself. i one time found a youtube video who did this but i lost it. The unity script reference docs are very confusing to me. They dont explain the core or something for me. but i might be missing that core. And so many tutorials on youtube they go… ok this part of code just copy paste… and im like… Thats the tutorial?? Copy pasting code? how about understanding it…
The Error Message
(leads to my mesh text script)
NullReferenceException: Object reference not set to an instance of an object FloatingTextSimple.Update () (at Assets/Scripts/FloatingTextSimple.cs:16)