- Home /
When online GUI.Label (or text area etc) shows overlapping text
You can see my small script below. It simply resets the players position when they fall below a certain height and adds one to their death count and also the team's death count.
using UnityEngine;
using System.Collections;
public class Scoring : MonoBehaviour
{
public float lowestFallDistance = -10;
private int deathCount;
public static int teamDeathCount;
private string deathCountString;
public static string teamDeathCountString;
private int screenWidth;
void Start()
{
deathCount = 0;
teamDeathCount = 0;
deathCountString = deathCount.ToString();
teamDeathCountString = teamDeathCount.ToString();
screenWidth = Screen.width;
}
void Update()
{
if(this.transform.position.y < lowestFallDistance)
Reset();
}
void OnGUI()
{
GUI.Label(new Rect(5, 5, 30, 20),deathCountString);
GUI.Label(new Rect(screenWidth - 35, 5, 30, 20),teamDeathCountString);
}
void Reset(){
this.transform.position = new Vector3(0,2,0);
++deathCount;
networkView.RPC("PlusTeam", RPCMode.AllBuffered, "");
deathCountString = deathCount.ToString();
}
[RPC]
void PlusTeam(string empty) {
++teamDeathCount;
teamDeathCountString = teamDeathCount.ToString();
}
}
The team's death count in the top right only shows once, and just as I intend it to, but the player's individual death count in the top left is always zero with the actual count displayed on top.
This only happens when I play online. If I play single player and die it just shows the correct score without a zero underneath. I really don't see what I've done wrong so any help would be greatly appreciated.
Answer by Dudledok · Jan 26, 2015 at 05:34 PM
Just went through some previous questions and seen this unanswered.
I had the script on more than one object so it showed it once for each object it was on. Sharing in case anyone else makes the same mistake I did.
Answer by MjrDeathAdder · Jul 22, 2014 at 09:24 AM
Not sure if this will fix it but it will reduce the amount of lines in your code instead of storing an individual string parallel to the integer why not when you need to display the counters as a string call the .toString() function.
GUI.Label(new Rect(5, 5, 30, 20),deathCount.ToString());
Reduce the amount of lines, yes, but not make it quicker. Your way would call ToString()
every cycle of OnGUI()
as opposed to just once each time the int
is updated.
Your answer
Follow this Question
Related Questions
How would you set up a Lobby Scene? 1 Answer
Display "organized" string on GUI Label 1 Answer
Trouble using C# tooltips 1 Answer
network GUI not working 1 Answer
How do you change the size and color of a GUI Label in C#? 4 Answers