- Home /
Populate UI Text with appropriate Network View
How would I initiate/change a local UI Text (or any) Element with a value from a Network-Instantiated prefab?
My basic setup is as follows, without any effect: Scene:
Camera
UiElement
Connection is established using MasterServer (Unity) - participants and visual effects are cloned from prefabs like so:
Network.Instantiate(playerPrefab, new Vector3(... etc.
state synchronization works properly as well.
The UI element has the following script attached to identify the value source:
public GameObject Player; //this is the player prefab
public Text energyDisplay; //this is the Text field inside the
void FixedUpdate () {
if(Player.GetComponent<NetworkView>().isMine){
energyDisplay.text = Player.GetComponent<PlayerStats>().energy.ToString();
}
}
Debug of "energyDisplay.text" as well as " Player.GetComponent().energy.ToString();" shows the correct value - however the text field does not change...
I also tried to instantiate a copy of the UI for each individual participant - check ownership and populate, but the result was just the same as above.
What am I doing wrong?
Have you tried just using Update ins$$anonymous$$d of just Fixed Update? If is YOUR "energy" then only YOU should see it right? Why not just attach this script to the player and just use GetComponent. It will take a bit less to do. Sorry, lol. I got off track. Just answer the first question xD
Thanks Legend,
I ended up assigning Text directly to the player - so I would mark your answer as correct if you post it as one. However, I'm still curious on how to get values from Gameobjects with NetworkView and add them to any local UI(or other element..)?
I'll post it as an answer then so others can see if it they have the same issue.
What are you trying to do? You want to get a value from a UI text and store it in a string or somewhere else?
basically as described above: NetworkInstantiated Gameobject -> read Value -> change Text on local UI
it works up until the local UI part...
as stated above I did and I made 100% sure that the targets were set properly. Although both energyDisplay as well as .energy.ToString()... were equal. I can even see the change in the editors text (Script) box - only thing not updating was the UI element on the view...
Answer by LegendWill · Mar 26, 2015 at 10:33 PM
Since it is YOUR "energy" then only YOU should see it right? Why not just attach this script to the player and just use GetComponent. It will take a bit less to do.
Hi Legend,
sorry for the confusion, I think I just now understood what you meant. However, this wasn't the solution that ended up working -
the only way I found to populate a local UI was to replace it with a Network.Instantiated one (is now initialized as a child of player) - and it works.
For some odd reason the reference to the local UI Element must be different once Network instantiated variables float around... is there a crack in the system? :)