Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hopper · Dec 05, 2016 at 07:31 AM · c#fpsaccessing from any script

Unable to access variable from other C# script?

I'm making a FPS and I have two scripts. "p_stats" is the statistics for the player (ammo, health, abilities, etc.) and "p_weaponmech" is the script that will change the gun based on class and make the gun fire. For some reason, though I have public variables, it completes the math equation from the p_stats script but fails to see the value in the p_weaponmech script.

p_stats

 public class p_stats : Monobehaviour
 {
       public int a_main;
       public int a_perclip;
       public int a_clips;
 
       void Start ()
       {
             a_main = a_clips * a_perclip;
             Debug.Log(a_main.ToString());
       }
 }

p_weaponmech

 public class p_weaponmech : Monobehaviour
 {
       private GameObject p_obj;
       private p_stats stats;
       private int p_ammo;
 
       void Start
       {
             p_obj = GameObject.Find("Player1");
             p_stats = p_obj.GetComponent<p_stats>();
             p_ammo = p_stats.a_main;
       }
 }

I've done several "Debug.Log" statements to see if the object was found, if the script was found, etc. and that apparently was not the problem. Any ideas?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by UnityCoach · Dec 05, 2016 at 02:01 PM

You assign p_stats, which is a type, not the member you declared. Try :

 stats = p_obj.GetComponent<p_stats>();
 p_ammo = stats.a_main;

I would also encourage you to follow C# naming conventions, class names begins with Upper case, members camelCase, private members begin with an _underscore, etc..

It should make things easier to catch. My two cents.

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 Bunny83 · Dec 05, 2016 at 02:43 PM 0
Share

The underscore for private members is not the official C# na$$anonymous$$g convention but quite popular. I also can't find any logical reason what "a_" and "p_" would mean in this context ^^.

I usually use the prefix "a" for arguments in methods. This simplifies the assignment of local variables with the same name ^^. I'm usually close to the "official" na$$anonymous$$g convention with a few exceptions.

avatar image christoph_r Bunny83 · Dec 05, 2016 at 03:22 PM 0
Share

Even though it contradicts the na$$anonymous$$g convention, I do like to tell at a glance if a variable is a member variable. I do see the issues with it, though, such as a higher chance of variable shadowing. Using it in type and member names seems odd, though.

avatar image hopper · Dec 05, 2016 at 10:18 PM 0
Share

I'm so dumb! I was coding this at midnight and I was so confused as to why it wouldn't work. Thanks so much for your help my dude!

avatar image UnityCoach hopper · Dec 05, 2016 at 11:30 PM 0
Share

no worries, happens to the best of us ;) That's why I suggested using a clear na$$anonymous$$g convention. It actually helps to spot these kind of mistakes.

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

9 People are following this question.

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

Related Questions

Access variables from other Script via Properties [C#] 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Script can't find Component within Start() - C# 1 Answer

Framerate doubles when pausing and unpausing editor 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