Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Memorix101 · May 07, 2014 at 03:28 AM · prefabmultiplayersendmessagenameplayername

Send last standing player name

Hello guys :)

I'm currently coding a lastman standing local multiplayer game.

I want to show a text after the match with the player who wins the match, but all I get is the name of the second player prefab (Player 2 is currently called Red) :/

That is my PlayerName Script ....

 using UnityEngine;
 using System.Collections;
 
 public class OutOfLevel : MonoBehaviour {
 
     public static GameObject GameOverUI;
     public static float PlayerActvie = 0f;
     public GameObject Explos;
     public TextMesh PlayerID;
     
     public string NameID = "Player 1";
 
     public static string SendNameID;
     // Use this for initialization
     void Start () {
         PlayerActvie += 1;
     }
     
     // Update is called once per frame
     void Update () {
         print("my name is " + NameID);
 
         if(!StartEvent.EventGo){
         GameObject.Find("UI").SendMessage("SendMsg", NameID);
         }
 
         PlayerID.text = "I'm " + NameID;
     }
 
     void CheckPlayerActive(){
         PlayerActvie -= 1;
         Destroy(gameObject);
     }
     
 
     void OnCollisionEnter(Collision collision) {
     
         if(collision.transform.tag == "OutOfLevel"){ 
             CheckPlayerActive();
             Instantiate(Explos,transform.position, transform.rotation);
         
 
         }
     
         }
 }
 

My receiver script.

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
 
     public static bool Singlepayer = false;
     public static bool Multiplayer = false;
 
     public GUIText WinText;
 
     private string WinTextInput = "Nothing received yet";
 
     // Use this for initialization
     void Start () {
         Singlepayer = false;
         Multiplayer = false;
     }
     
     // Update is called once per frame
     void Update () {
        
 
         if(StartEvent.EventGo && Multiplayer){
             WinText.text = "Victory for " +  WinTextInput + " !";
         }
 
     //    print ("s"+Singlepayer + "m" + Multiplayer + "pa" + OutOfLevel.PlayerActvie);
 
     }
 
     void SendMsg(string meep){
 
          WinTextInput = meep;
 
     }
     
 }

Thank you for your help :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kacheek · May 07, 2014 at 08:28 AM

hmm i would recomand to just go for a tag search... just add the tag "player" to your player prefab and then sync it with an rpc call (i dont see any in your script how do you pass the information over the network?) also you should assign the players name to the players gameobject character just like so :

 gameObject.name = playername;


well heres some code you could use :

 //create the variables
 GameObject[] players;
 string lastplayername = "";
 string thelastplayer = "";
 
 // check if the variable has been assigned yet if so output the playername
 if(thelastplayer != "")
 {
  print("my name is " + thelastplayer);
 debug.log ("my name is " + thelastplayer);
 }
 //just let the server do this
 if(Network.peerType == NetworkPeerType.Server )
         {
 //create an array with all players that have the tag "player"
 players = GameObject.FindGameObjectsWithTag("player");
 
 //if there only is one player
 if(players.Length == 1)
 {
 //save the name of the last player
 lastplayername = (players[1]);
 //send the RPC to your clients so they know his name 
 networkView.RPC("telleveryonewhothelastplayeris",RPCMode.All, lastplayername);    
 }
 }
 [RPC]
     void telleveryonewhothelastplayeris (string thename)
     {
 // assign the name to the final variable
 
 thelastplayer = thename;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Memorix101 · May 07, 2014 at 11:38 AM

Thank you :) Yes, finally I made it the same :)The weird thing was the first time it didn't work, so I was so frustrated i close Unity. Later today, I reopened it and the code I posted below works instantly O.o Weird thing. THank you for your help :)

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
 
     public static bool Singlepayer = false;
     public static bool Multiplayer = false;
     public GameObject player;
     public GUIText WinText;
 
     private string WinTextInput = "Nothing received yet";
 
     // Use this for initialization
     void Start () {
         Singlepayer = false;
         Multiplayer = false;
         player = null;
         StartEvent.EventOver = false;
         //player = transform.GetComponent<OutOfLevel>();
     }
     
     // Update is called once per frame
     void Update () {
 
         print(GameObject.FindWithTag("Player").name);
 
         if(StartEvent.EventOver && Multiplayer){
            
             if(!player){ player = GameObject.FindWithTag("Player");}
 
             WinText.text = "Victory for " + player.GetComponent<OutOfLevel>().NameID + " !";
 
             if(player.GetComponent<OutOfLevel>().NameID == "Red"){
                 WinText.color = Color.red;
             }
             
             if(player.GetComponent<OutOfLevel>().NameID == "Blue"){
                 WinText.color = Color.blue;
             }
         }
 
     //    print ("s"+Singlepayer + "m" + Multiplayer + "pa" + OutOfLevel.PlayerActvie);
 
     }
     
 }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kacheek · May 07, 2014 at 01:51 PM 0
Share

no problem ;)

avatar image Kacheek · May 07, 2014 at 02:44 PM 0
Share

by the way if noticed that you use GameObject.FindWithTag this will only output one object! not if ther is just one object even if there are 10 players youll get just one of them with the code ive wrote above ittl check if there is just one player its creating an array which will first assign all player names to the array and then check how big that array is i recomand to do that!

if you want a variable to be an array use GameObject[] ins$$anonymous$$d of GameObject ... this [] defines the array!

i hop i could help

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiplayer - Opposite names above players 1 Answer

How do I scale my prefab over the network? 0 Answers

(C#) Help with Multiplayer Features - Photon Unity Networking 0 Answers

In Unity can you make games that have online features and how can you test it to see if it works 2 Answers

Instantiate already connected players. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges