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 EClancy · Jun 19, 2014 at 05:03 AM · javascriptgameobjectvariablescope

Scripts accessing one another (JS)

I have looked at a ton of similar questions for hours today, and I cannot figure out why my problem is different, or what detail I'm missing.

I have a script for each of several players to select a character, and I simply want to make sure no one chooses the same character. Please excuse how verbose it is. I know I have extra stuff in here - it's been mangled throughout the day, but what I really need is what's giving me this specific error.

I have a variable:

 public static var P1Selected = null;

This holds the choice this player has made (an int).

This is used to get the object, and a reference to the script.

 Player2 = GameObject.Find("Player2");
 P2script = Player2.GetComponent(P2Select);


I also have a get method.

 public function getSelected(){
     return P1Selected;
 }

This is to allow other scripts to inquire the value. I have also simply tried accessing the variable, but I find the same problem.

Finally, this is the line where I test.

 if(P2script.getSelected() != CharIndex &&      
    P3script.getSelected() != CharIndex && 
    P4script.getSelected() != CharIndex){

       //successfully lock in character

    }

It continuously tells me that the variable and the function do not exist in the other script. I have also tried declaring it as

 P2script : P2Select = Player2.GetComponent(P2Select);

To which the compiler responds "unexpected token". Saw that in some other examples. I also saw people mentioning dropping one script in standard assets, because it compiles first. This does not help, as they'll be changing at runtime and also need to access each other.

I feel like I've missed something small and need another pair of eyes to spot an obvious mistake.

Comment
Add comment · Show 4
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 robertbu · Jun 19, 2014 at 05:11 AM 0
Share

It is unclear how all your variables are defined. It might be easier to figure out if you put all these scripts on PasteBin and provided a reference. Note that since 'P1Selected' is a static variable, you will have only a single instance shared between all the script, and you can access it by the Class (no need for GetComponent(). Not sure if that is the way it needs to be structured or not.

avatar image Kiwasi · Jun 19, 2014 at 06:35 AM 0
Share

Not sure how everything ties together. Try posting complete classes. Feel free to strip everything out that doesn't touch the mentioned variables.

You should also consider using an array ins$$anonymous$$d of hardcoding the player numbers.

avatar image tanoshimi · Jun 19, 2014 at 06:45 AM 0
Share

You have four essentially identical scripts called P1Select, P2Select, P3Select, P4Select ?

avatar image haico1992 · Jun 19, 2014 at 06:53 AM 0
Share

Can you please kindly set that P2Select to public?

0 Replies

· Add your reply
  • Sort: 

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

Acces to a var from another Script. 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Problem with PlayerHealth and renderer 1 Answer

variable = true from another script 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