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 LukeDodds · Feb 18, 2015 at 10:00 AM · javascriptbugvariable

playerID does not exist in current context?

void Awake () { players = new GameObject[numPlayers];

     Player PlayerScript;
     PlayerScript = players[playerID].GetComponent<Player>();
     PlayerScript.playerID = playerID;
 }


not sure why this isnt working, as a playerID variable is in the Player script.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DiegoSLTS · Feb 18, 2015 at 12:42 PM

playerID is just a variable on PlayerScript? You're using a playerID variable too that should be defined on the same script.

What line gives you the error?

 PlayerScript = players[playerID].GetComponent<Player>();

or

 PlayerScript.playerID = playerID;

?

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 LukeDodds · Feb 19, 2015 at 11:34 AM 0
Share

PlayerID is just a variable from Player script i was trying to get, I got both lines in error, one for each PlayerID

avatar image DiegoSLTS · Feb 19, 2015 at 12:54 PM 0
Share

You need to read more about scopes and how public variables work. A public variable doesn't mean you can write it's name anywhere, it could be a public instance variable (so from outside the class you need to have a reference to that instance and call instanceOfClass.playerID) or a public class variable (so you have to write ClassName.playerID).

In your case you're just writing "playerID", so the compiler assumes it's a public variable of the same script, it won't use the public variable defined in other classes.

You need a variable on that script (NOT PlayerScript) to be able to write those lines, and from the little code you posted it looks like that's what you actually wanted.

Some questions that might help you understand the problem in your code: What value should playerID have when Awake is called? How is that value set?

avatar image
1

Answer by alok-kr-029 · Feb 19, 2015 at 12:45 PM

Try debugging this then proceed further

 Player PlayerScript = players[playerID].GetComponent<Player>();
     
 Debug.Log(PlayerScript.playerID);

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 giulio-pierucci · Feb 18, 2015 at 10:22 AM

Player class must have a public field playerID

public int playerID = 0;

Comment
Add comment · Show 1 · 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 LukeDodds · Feb 19, 2015 at 11:33 AM 0
Share

It does have a public field, but in the Player script "public int playerID; " Should it be in the gamemanager ins$$anonymous$$d/aswell?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Issues accessing a variable in another script 0 Answers

Cannot implicitly convert type `void' to `float' 1 Answer

Getting variable from another script 2 Answers

Function in variable (UnityScript) 1 Answer

GetComponent from class 1 Answer


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