Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Player Score and Server Score?

Discussion in 'Scripting' started by wwm0nkey, Aug 14, 2015.

  1. wwm0nkey

    wwm0nkey

    Joined:
    Jul 7, 2015
    Posts:
    42
    Okay so I posted this over in networking but I fixed my spawning issue and now I have one last issue that I am having a hard time getting my head around.

    I need 2 separate scores and the current way it is set up is that a trigger box stores the scores of what people put into it so that's the server score. Then everyone has their own personal score for each coin they toss into the trigger box. At the end of a round if the server score is above 4 each players personal score is multiplied by 1.6.

    So I know how it should work, but the problem right now is I can't communicate between the two scripts properly. I thought maybe doing a Master Script where both took from it would also update the values in it, but that's not working so I need to find another way around this. So if anyone has any suggestions or solutions it would be a big help :)

    Right now the way it looks is pretty much

    PlayerScore <==> MasterScript <==> ServerScore
     
    Last edited: Aug 14, 2015
  2. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    If the PlayerScore script is written in the same language as the ServerScore script you just access information from that script via code:
    Code (csharp):
    1.  
    2. // The reason why we dont use the "direct" refrence is because you want to grab the script
    3. // of the player gameobject at runtime while it's being modified.
    4. GameObject go_Player = GameObject.Find("player_gameobj_name");
    5. PlayerScore ps = (PlayerScore) go_Player.GetComponent(typeof(PlayerScore));
    6. // You can access the variable directly or use a get function.
    7. ps.getCurrentCoinScore();
    8.  
    Adapted from this Unity answer.
    http://answers.unity3d.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html

    With this you can just compare the scores to the ServerScore and just update the PlayerScore or whatever you need to update the sameway.

    Code (csharp):
    1. if (ps.getCurrentCoinScore() > 4) {
    2. // Or whatever number you specifiy
    3. // Do a thing.
    4. }
    Hopefully this helped.