- Home /
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?
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?
Actually, from memory when I last used C#, the class had to be static too... I may be wrong (usually).
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
?
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.
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
PlayerAtts isnt held by any object.. its a custom class script (like public class PlayerAtts { etc)
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.
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
![](https://koobas.hobune.stream/wayback/20220613133802im_/https://answers.unity.com/themes/thub/images/avi.jpg)