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?

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?

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.