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 /
This question was closed Nov 29, 2014 at 06:20 PM by fafase for the following reason:

Other

avatar image
1
Question by lamecheesykiwi · Nov 28, 2014 at 04:57 PM · photongetcomponentstringrandom.range

How do I use Get Component in this code

I asked for help getting a random player system and they gave me the code below, but TaggedPlayer is a string. I need to be able to enable a script on the random player. How do I make it where I can use .GetComponent on the TaggedPlayer? Please help, I've been stuck on this part of my code for 1 whole month now...

 public List<string> PlayerList = new List<string>();
 
 void OnPhotonPlayerConnected(){
             if (!PlayerList.Contains (PhotonNetwork.playerName)) {
                    PlayerList.Add (PhotonNetwork.playerName);
             }
       }
 
 void TagRandomPlayer(){
       int PlayerListRange = PlayerList.Count;
       System.Random Rand = new System.Random ();
       int RandomPick = Rand.Next (0, PlayerListRange);
       string TaggedPlayer = PlayerList [RandomPick];
       }
Comment
Add comment · Show 1
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 fafase · Nov 29, 2014 at 06:23 PM 0
Share

There are actually two issues in your question, the first one, you are asking about a topic that has many threads already, some of them including detailed explanation.

http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

Second, you start your question with "they gave me the code below".

Those two indicates that you need to spend more time learning. Unity has a large section for beginner tutorials, you should use it:

http://unity3d.com/learn/tutorials/modules/beginner/scripting

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by zak-reynolds · Nov 28, 2014 at 06:16 PM

The GameObject that is controlled by each player and the actual PhotonPlayer are actually two separate objects, and the problem you have is that they're not linked together in some way. What you could do is add a Dictionary so that it can keep track of the controlled GameObjects. It's a little hard to tell what the best way to populate the dictionary without more detail, but what I often do is have the main script that drives the players' GameObject "register" with the main NetworkManager script. So in the PlayerInput's (or whatever the script may be) Start() method:

 string myPlayerName = photonView.owner.name;
 NetworkManager.RegisterPlayerObject(name, this.gameObject);

Then in NetworkManager (which seems to be the function of the script you posted above):

 Dictionary<string, GameObject> PlayerDictionary = new Dictionary<string, GameObject>();
 
 public static RegisterPlayerObject (string playerName, GameObject playerGameObject) {
     NetworkManager nm = GameObject.Find("NetworkManager").GetComponent<NetworkManager>();
     nm.PlayerDictionary.Add (playerName, playerGameObject);
 }

Then when you want to get the GameObject for a particular player (or a component on that GameObject):

 // Code from OP...
 string TaggedPlayer = PlayerList [RandomPick];
 // For example, get the PlayerInput component:
 PlayerInput pi = PlayerDictionary[TaggedPlayer].GetComponent<PlayerInput>();

I hope that helps! You could also look into the TagObject property of PhotonPlayer (http://doc-api.exitgames.com/en/pun/current/pun/doc/class_photon_player.html#aaf54b32878a605d3e4d47f16ad106aa3), but I haven't had much success with it personally.

Comment
Add comment · Show 4 · 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 lamecheesykiwi · Nov 29, 2014 at 03:19 AM 0
Share

Where did you get PlayerInput from? I'm getting errors on public static RegisterPlayerObject and Network$$anonymous$$anager.RegisterPlayerObject(name, this.gameObject);

EDIT: Fixed the errors for them... Put a void after static.

avatar image lamecheesykiwi · Nov 29, 2014 at 04:00 AM 0
Share

How do I activate the code? I'm using this to enable some code on a character and I can't seem to activate it. When I try to it says Assets/The$$anonymous$$urderer/Scripts/Network$$anonymous$$anager.cs(33,14): error CS1624: The body of Network$$anonymous$$anager.OnNewRound()' cannot be an iterator block because void' is not an iterator interface type

avatar image zak-reynolds · Nov 29, 2014 at 05:46 PM 1
Share

Ah sorry, it wasn't meant to really be copy/pas$$anonymous$$ble and more a demonstration of the idea, and yeah the missing void was a typo on my part. "PlayerInput" is just the class name I use on my own player GameObjects, it should be changed to what you use (the analogous class in the $$anonymous$$arco Polo tutorial is called "NetworkCharacter"). Basically, the idea is that when each player's object is instantiated in your scene, it registers itself with your Network$$anonymous$$anager so you can use it through the dictionary.

As far as the error CS1624 that you're getting now, it doesn't seem to be related to the code above; are you using yield in that method? I found this forum post that might help you out with that error.

Also as a side note that I just noticed looking at it again, when you use OnPhotonPlayerConnected, I'm fairly certain it's only called for the client it's running on and each client that connects afterwards, but not any that are already in the game. So if you have 4 players (A, B, C, and D who connect in that order): A will have it called for all players; B will have it called for B, C, and D; C will have it called for C and D; and D will only have it called for itself. That's why I like to use the "registering" method above, and you could add

 nm.PlayerList.Add(playerName);

to the RegisterPlayerObject method to avoid that problem with your list as well.

avatar image lamecheesykiwi · Nov 29, 2014 at 07:49 PM 0
Share

This is probably obvious, but I'm new to program$$anonymous$$g and the there's a problem with it that I can't figure out. Right now I'm just trying to make sure it works by using a yield and there's no errors, but it's not enabling my code. Here's some code from network manager that I'm trying to use to select a random person. What's wrong with this?

     public List<string> PlayerList = new List<string>();
     
     public bool offline$$anonymous$$ode = false;
 
     Dictionary<string, GameObject> PlayerDictionary = new Dictionary<string, GameObject>();
     
     public static void RegisterPlayerObject (string playerName, GameObject playerGameObject) {
         Network$$anonymous$$anager nm = GameObject.Find("$$anonymous$$anager").GetComponent<Network$$anonymous$$anager>();
         nm.PlayerDictionary.Add (playerName, playerGameObject);
         nm.PlayerList.Add(playerName);
     }
 
     void OnPhotonPlayerConnected(){
         if (!PlayerList.Contains (PhotonNetwork.playerName)) {
             PlayerList.Add (PhotonNetwork.playerName);
         }
     }
     void TagRandomPlayer(){
         int PlayerListRange = PlayerList.Count;
         System.Random Rand = new System.Random ();
         int RandomPick = Rand.Next (0, PlayerListRange);
         string TaggedPlayer = PlayerList [RandomPick];
         $$anonymous$$nife knife = PlayerDictionary [TaggedPlayer].GetComponent<$$anonymous$$nife>();
         (($$anonymous$$onoBehaviour)knife.GetComponent("$$anonymous$$nife")).enabled = true;
     }
     IEnumerator OnNewRound(){
         yield return new WaitForSeconds(10);
         TagRandomPlayer ();
         Debug.Log ("Hello");
     }
     // Use this for initialization
     void Start () {
         spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
         Connect ();
         OnNewRound ();
     }

 

Follow this Question

Answers Answers and Comments

27 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a String to populate GetComponent 1 Answer

Using Random.Range in an array only gives a random output once 2 Answers

Is it possible: "getComponent<_array_element_$>"? 1 Answer

GetComponent(string) with unknown ScriptName 2 Answers

Random String from list of string without repeated letters 2 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