- Home /
A better way to stop sending updates to another object.
This is what I'm currently doing. I have two GUITexts as children of a GUITexture that gets updates from a gameobject that is clicked on. When the gameobject gets clicked on, it uses a reference I've set up in the Inspector to send its information to the GUITexture. The GUITexture then updates its children with the information that was provided. The gameobject keeps updating the information by calling a public function in the GUITexture's script.
The problem is that when a different gameobject is clicked I want the old one to stop updating the GUITexture so the new one can. I'm not sure how to do this easily and my solution doesn't seems quite right.
The way I'm fixing this is setting up references to every thing that can update the GUITexture, having a "menu number" being sent to the GUITexture and checking them to grab the correct information from the reference script. You can also click on objects that are initiated later in the game by also sending the object's name and then looking it up with gameobject.find, then grabbing it's script and then grabbing the information for the two GUITexts.
My hopes were that it's somehow possible to send a reference of the last script that called the public function and using that reference to tell it to to stop.
Any ideas? The scripts below don't have the fixes I mention above as I just thought of them and wanted to check with you guys before I implemented them.
Some C# script: The GUITexture
public class GUIInfo : MonoBehaviour { public DisasterArea disasterArea; public Hospital hospital; public ScoutTeam scoutTeam; public MedicSite medicSite;
public GUIText type;
public GUIText data;
private int currentNumber;
private string currentName;
void Start()
{
currentNumber = -1;
}
// Update is called once per frame
public void ChangeTypeData(string msgName, int msgNumber, string msgType, string msgData)
{
if(currentNumber != msgNumber && currentNumber != -1)
{
if(currentNumber == 0)
disasterArea.StopSendInfo();
if(currentNumber == 1)
scoutTeam.StopSendInfo();
}
currentName = msgName;
currentNumber = msgNumber;
type.text = msgType;
data.text = msgData;
}
}
-----The object that calls-------
public class DisasterArea : MonoBehaviour { public GUIInfo guiInfo; public int wounded;
private bool sendInfo;
private int tempWounded;
void Start ()
{
sendInfo = false;
tempWounded = 0;
}
void Update()
{
if(sendInfo && tempWounded != wounded)
{
guiInfo.ChangeTypeData(gameObject.name, 0, "Wounded", wounded.ToString());
tempWounded = wounded;
}
}
void OnMouseDown()
{
guiInfo.ChangeTypeData(gameObject.name, 0, "Wounded", wounded.ToString());
sendInfo = true;
}
public void StopSendInfo()
{
sendInfo = false;
}
}
edit: Hazaa! I figured out the code snippet stuff!
edit2: Updated Code to show the fixes, it seems to work well. Gonna try the vehicles next as they are instantiated.
Hard to say, but it looks like you're updating the GUI constantly. I'd be tempted to try and make it event driven, but it's hard to say how this might look for you, without understanding your situation better.
Here's an example of what we do: We have health and power in our GUI and ins$$anonymous$$d of constantly watching the value of the players health, we subscribe to C# events on the player's PlayerCharacter class which are triggered when the health or power change and we only update the GUI when some change has occurred.
I don't know if that might help you think of a different way to do what you're doing, but otherwise if you can describe what is going one from the player's perspective, that might help my understanding a bit.
Yeah, I know I'm not explaining myself too well. The game would be close to a Sim's type game, very loosely though. There isn't a "player" but you use types of currency to buy and upgrade stuff. This part of the code deals with getting that objects information and having it update it and stop when a new item is clicked.
I did add a check to only send the message if the variable changes. Thanks.