I have a simple questions.
I have two file script :
Player.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class player {
public List<item> itemx = new List<item> (); // Here
}
File Inventory.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class inventory : MonoBehaviour {
public player playerx;
itemDatabase database;
// Use this for initialization
void Start () {
database = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<itemDatabase> ();
//Generate the Slot and Slot Name;
for(int i = 1; i <= 24; i++) {
playerx.itemx.Add(new item()); // Here
playerx.itemx[i] = database.items[i]; // And Here
}
}
}
As you can see in player.cs file i have declare a list variable :
public List<item> itemx = new List<item> (); // Here
and in Inventory.cs file i want to add the value using :
playerx.itemx.Add(new item()); And Here
is that possible to save the value variable to player.cs file ?
Thanks
in your question, you have
public class Player
instead of
public class Player:MonoBehaviour
I assume that's just a typo. If you're trying to have a "free" class Player (as in an OO environment), you can't do that.
Correct, there is absolutely on problem with doing this!
A couple of things...
Note that in the second script it would be:
public player playerx; ... WRONG
public Player player; ... CORRECT
and then just
player.items.Add( .. etc )
(Don't forget that of course you must drag to connect the "Player player" inspector variable. If you do not know how to do that say so and I will give you a link to a tutorial.)
Secondly, in the first script you have a problem.
There's a stupid thing in Unity where "public" means "inspector variable"
In fact you want an "ordinary" public variable as in any ordinary programming language, strangely enough you have to type this
it's just one of those weird things about Unity. In fact in many projects you never use inspector variables. So you just constantly type "[System.NonSerialized] public" everywhere. The long one "[System.NonSerialized] public" is sort of the "normal" one if you see what I mean. Use it all the time. Only when you especially want an inspector variable, use "public".
public List<item> itemx... WRONG
[System.NonSerialized] public List<item> items... CORRECT