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 thornekey · Feb 17, 2014 at 09:46 AM · c#variableaccess

Accessing Variable from another Script.. with a twist

Hi guys, so i have a script called 'PlayerAtts'

 public class PlayerAtts {
 
     //Player 
     public string PlayerName;
 
     
     //Skills
     public int Health;
     public int Craft;
     public int Combat;
     
     
 
 }

and this script has all the details about it throughout my game. Now i have a scene where you can create your character.. i have a script called 'CharacterCreation' i dont want to use charactercreation and playeratts in the same script cos its just not good..

so in the 'CharacterCreation' script it says:

     public List<PlayerAtts> PlayerInfo;
     public PlayerAtts[] Info;
     void Awake () {
         PlayerInfo = new List<PlayerAtts>();    
     }
     // Use this for initialization
     void OnGUI () {
         GUI.Box(new Rect(0,0,512,32), "Character Creation");
         PlayerAtts.PlayerName = GUI.TextField(new Rect(0,0,512,32), PlayerAtts.PlayerName, 25);
         
     }
 

as you can see in the CC script i want to have a text box which you can enter your name.. although im getting this error:

 An object reference is required to access non-static member `PlayerAtts.PlayerName'


what am i doing wrong?

Comment
Add comment · Show 6
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 POLYGAMe · Feb 17, 2014 at 09:50 AM 0
Share

I'm not up with C# but usually that simply means you need to set whatever it is you are accessing to static. So maybe the variables in your PlayerAtts class need to be static?

avatar image POLYGAMe · Feb 17, 2014 at 09:56 AM 0
Share

Actually, from memory when I last used C#, the class had to be static too... I may be wrong (usually).

avatar image thornekey · Feb 17, 2014 at 10:02 AM 0
Share

oh so the variable static?

avatar image thornekey · Feb 17, 2014 at 10:17 AM 0
Share

for some reason it just makes the name variable just not show up...

alt text

picture 54.png (8.5 kB)
avatar image whydoidoit · Feb 17, 2014 at 11:10 AM 0
Share

Why have you got a list of these PlayerAttrs?

Show more comments

2 Replies

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

Answer by vexe · Feb 17, 2014 at 11:45 PM

That simply means that you have a non-static method/variable/field, etc that you're trying to access in a non-static fashion. PlayerName is a member, which means you need to have a PlayerAtts object for it to act upon, like:

 PlayerAtts p = new PlayerAtts();
 p.PlayerName = "playa";

Or could be one of the elements of your array/list:

 Info[index].PlayerName = ...;

If you want to fetch the PlayerAtts from a gameObject, then PlayerAtts needs to be a MonoBehaviour for your to be able to attach it to the gameObject, not just a regular C# class.

Also, why do you need both an array and a List of PlayerAtts?

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 Nick4 · Feb 17, 2014 at 10:04 AM

If they are held by the same game object, do this :

 void OnGUI () {
        GUI.Box(new Rect(0,0,512,32), "Character Creation");
        PlayerAtts.PlayerName = GUI.TextField(new Rect(0,0,512,32), GetComponent<PlayerAtts>().PlayerName, 25);     
     }

If they are not, you need to get the object that is holding PlayerAtts script :

 GameObject go; // OBJECT THAT HAS PlayerAtts SCRIPT
 
 void Start () {
        go = GameObject.FindGameObjectWithTag("Tag of go object");
     }
 
 void OnGUI () {
        GUI.Box(new Rect(0,0,512,32), "Character Creation");
        PlayerAtts.PlayerName = GUI.TextField(new Rect(0,0,512,32), go.GetComponent<PlayerAtts>().PlayerName, 25);
  
     }

Good luck.

Comment
Add comment · Show 5 · 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 Nick4 · Feb 17, 2014 at 10:06 AM 0
Share

As POLYGA$$anonymous$$e stated above, you can also define variables static so that when you create an object from that class, you can use it like :

 $$anonymous$$yClass.myStaticVariable
avatar image thornekey · Feb 17, 2014 at 10:22 AM 0
Share

PlayerAtts isnt held by any object.. its a custom class script (like public class PlayerAtts { etc)

avatar image thornekey · Feb 17, 2014 at 11:18 PM 0
Share

bumpedy bump; still havent sorted this..

avatar image vexe · Feb 17, 2014 at 11:48 PM 0
Share

Static variables are not meant to be used like that, See this. Skip to the good coding habits section. Please stay away from static unless you know what you're doing.

avatar image getyour411 · Feb 17, 2014 at 11:55 PM 0
Share

If you aren't going to inherit from $$anonymous$$onoBehaviour, then I don't think you can use things like Start(), Awake(), OnGUI()

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

23 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

Related Questions

how to acess Static variable in other scripts without extended functions? 2 Answers

Access a variable easily from anywhere 1 Answer

Can't access a javascript static variable from c# script 1 Answer

Access a variable from another Script inside IF 1 Answer

Change and Access another scripts Variables 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