GUI problems

Updated on June 4, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
21 on June 4, 2017

Okay so I’m trying to do a timer, yet every time I go into play mode it wipes my reference to the timer text.

This is where I keep getting null reference(timerCountDown.text = timer.ToString ();), yet I’ve set the reference outside of game mode, but it wipes that during. If I manually put it back during game mode it works fine, but of course that’s not saved.

 

using UnityEngine;
using UnityEngine.UI;
public class PlayerUI : MonoBehaviour {

private float timer = 10f;
[SerializeField]
private Text timerCountDown;

void Start ()
{

timerCountDown = GetComponent<Text> ();

}

void Update()
{
MatchTimer ();
}

public void MatchTimer()
{
timer -= Time.deltaTime;
timerCountDown.text = timer.ToString ();
if (timer <= 0)
{

}
}
}

 

I have nothing for when it gets to zero yet. I will be calling a method to end match though. Still gotta work that out. I have the end match function, just not calling it properly. Anyone know what I’m doing wrong and can help please?

  • Liked by
Reply
1 on June 4, 2017
void Start ()
{
timerCountDown = GetComponent<Text> ();
}

Remove that, the reference gets overwritten.

on June 4, 2017

Thanks, I didn’t realise I was overwriting it. Much appreciated.

Show more replies
  • Liked by
Reply
Cancel
0 on June 4, 2017

So I had to rework it, so the game manager runs it and I’m still getting a null reference. Funny things is though, it works as intended, but I’m not sure how to remove the error. Sorry for being annoying, I know it’s a similar issue as before, but I’m not sure why it’s happening this time. 

 

Error:NullReferenceException: Object reference not set to an instance of an object
GameManager.MatchTimer () (at Assets/Scripts/GameManager.cs:57)
GameManager.Update () (at Assets/Scripts/GameManager.cs:47)

 

New code:

 

private NetworkManager networkManager;

public MatchSettings matchSettings;

private float timer = 120f;

 

void Awake ()
{

networkManager = NetworkManager.singleton;

}

 

void Update()
{
MatchTimer ();
}

public void MatchTimer()
{
var timerCountDown = GameObject.FindWithTag(“Time”).GetComponent<Text>();
timer -= Time.deltaTime;
timerCountDown.text = timer.ToString ();
if (timer <= 0)
{
LeaveRoom ();
}
}

public void LeaveRoom ()
{
MatchInfo matchInfo = networkManager.matchInfo;
networkManager.matchMaker.DropConnection(matchInfo.networkId, matchInfo.nodeId, 0, networkManager.OnDropConnection);
networkManager.StopHost();

}

  • Liked by
Reply
Cancel
12 on June 4, 2017

Can you show me which lines throw the errors. And also, finding the text game object every frame update is a bit too much, don’t you think. Finding an object is taxing on it’s own when called once, not EVERY SINGLE FRAME.

on June 4, 2017

Yeah that’s true. I had it in awake, but took it to try and stop the error.

It’s a weird error as the function still happens, but I get this red error, yet my timer counts down and the scene ends when the timer does. 

 

This is the line the error takes me to:

timerCountDown.text = timer.ToString ();

on June 4, 2017

[SerializeField]
private Text timerCountDown;

 

and the find object is back in awake

on June 4, 2017

Bah this is weird, it’s no different to the first time I had it, but now the error is throwing to this line and in awake. It also stopped working.

 

 timerCountDown = GameObject.FindWithTag(“Time”).GetComponent<Text>();

 

I can’t just comment this out either, because the timer is on the player ui, which gets instantiated into the match. Perhaps if I put the countdown part of the ui in the scene with the game manager and take just that part of the ui out from the player, will that work, can I have a ui on the player and in the scene?

Wise
on June 4, 2017

Try to put it back to awake again. And don’t use var, use the actual type.

Wise
on June 4, 2017

The problem is that it can’t find the text game object in the scene. Maybe the command only looks under the scripts object.

on June 4, 2017

So to be clear I have a game manager object with a script. This script shown here to be exact. 

I also have a player prefab with the ui on it and the text that I’m trying to get.

The game manager is always in this scene, but the player gets spawned with the network manager.

I enter this scene from a lobby. 

The idea is for the text to be the matches/scenes time limit. Which end of the countdown takes you back to the lobby.

 

So what would be your suggestion for how to get the game manager to get the text on the player? 

Wise
on June 4, 2017

Is the player instantiated at the start? Also finding the game object in awake wouldn’t work correctly, it should be done in start.

on June 4, 2017

Yeah, I’ll try that and see.

on June 4, 2017

Still null reference.

Wise
on June 4, 2017
private Text timerCountDown;
private void Start ()
{
    timerCountDown = GameObject.FindWithTag ("Time").GetComponent<Text> ();
}
private void Update ()
{
    if (timerCountDown == null)
        timerCountDown = GameObject.FindWithTag ("Time").GetComponent<Text> ();
}

If at start it is null it will check again and again until it finds it. This is really bad way of doing it, but try it anyways.

Show more replies
  • Liked by
Reply
Cancel
4 on June 4, 2017

Oh is it because the player gets spawned in and the gamemanger is always  in the scene? So for a brief bit of time, the game manager is there without a player and therefore it doesn’t work on awake or start.

Wise
on June 4, 2017

Exactly, it tries to find the text which isn’t in the scene until the player spawns.

on June 4, 2017

Maybe a delay on the start before calling it?

Wise
on June 4, 2017

You need some kind of a callback to sit on the player. Maybe check out events.

on June 4, 2017

oki thank you, I will do that.

Show more replies
  • Liked by
Reply
Cancel