C# access a generic list from another controller
Hello,
i am very new to unity and i cant solve this Problem:
public class IodeController : MonoBehaviour {
private PlayerController player;
public ResourceModel IronModel;
public void OnTriggerEnter(Collider other)
{
Gather(other);
}
public void OnTriggerStay(Collider other)
{
Gather(other);
}
public void OnTriggerExit(Collider other)
{
Debug.Log("Left Trigger");
}
public void Gather(Collider other)
{
player = other.GetComponent<PlayerController>();
if (Input.GetButton("Interact"))
{
Debug.Log("i am In");
var ironOre = new ResourceModel("Iron Ore", "Ore");
Debug.Log(ironOre.Name);
Debug.Log(player.Inventory.Count);
}
}
}
this is the other class:
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float rotationspeed;
CharacterController cc;
AnimationCurve ac;
private Animation aa;
public List<ResourceModel> Inventory;
// Use this for initialization
void Start ()
{
cc = GetComponent<CharacterController>();
aa = this.GetComponent<Animation>();
Inventory = new List<ResourceModel>();
Debug.Log(Inventory);
}
// Update is called once per frame
void Update ()
{
transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal") * rotationspeed * Time.deltaTime, 0));
Vector3 forward = new Vector3();
if (Input.GetButton("Vertical") && Input.GetButton("Sprint"))
{
aa.Play("run");
forward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * (moveSpeed * 4);
}
else if (Input.GetButton("Vertical"))
{
aa.Play("walk");
forward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * moveSpeed;
}
else
{
aa.Play("idle");
}
cc.Move(forward * Time.deltaTime);
cc.SimpleMove(Physics.gravity);
}
public void AddItem(ResourceModel model)
{
this.Inventory.Add(model);
this.Inventory.ForEach(x => Debug.Log(x.Name));
}
public ResourceModel GetItem(string itemName)
{
return this.Inventory.SingleOrDefault(x => x.Name.Equals(itemName));
}
}
as you can see i want to set a new Instance of ResourceModel to the Inventoy, which i created on the Start function on the PlayerController. The INstance is there as aspected. But if i try to access it on the GatherOre Function the List is null.
Can somebody explain that? to me?
Hello!
Are you saying that this line writes 0 or it throw a null exception?
Debug.Log(player.Inventory.Count);
Hi,
it´s a null Exception. A 0 would better, but sadly its a null reference.
The truth is, a null exception is better. Because the error is obvious while 0 is weird.
If it is a null exception, it would mean 2 things:
Whether player or player.Inventory is null.
If player is null, it says that:
player = other.GetComponent();
The Collider other does not have PlayerController in it. Check the GameObject of the Collider.
If player.Inventory is null, it is worst.
I think i got an idea about the Problem.
The "other" is my Character, which only have a CharacterController and no collider. So the Collider "other" is null. Do you have an advice to solve that?
Hi,
i found a solution:
$$anonymous$$y CharacterController is working as a collider as aspected. and the $$anonymous$$eshCollider doesn´t work somehow so i added a Box Collider on it without "trigger" and now it works perfectly. THanks for your help!