I am trying to set up transferring items between gameObject, the items are only numbers in code so no game Objects are being transferred but am gettingL
NullReferenceException: Object reference not set to an instance of an object
infoPawn..ctor (System.String newFirstName, System.String newLastName, Int32 newHP, Int32 newLuck, Int32 newStrength, Int32 newCharisma, Int32 newIntelligence, Int32 newDexterity, Int32 newPerception, Int32 newXP) (at Assets/Scripts/infoPawn.cs:60)
objectPlayer.Start () (at Assets/Scripts/objectPlayer.cs:13)
And can’t see why.
Here is infoPawn:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class infoPawn : MonoBehaviour {
public string firstName;
public string lastName;
public string profession;
public int HP;
public int luck;
public int strength;
public int charisma;
public int intelligence;
public int dexterity;
public int perception;
public int resource;
//
public int curHP;
public int curLuck;
public int curStrength;
public int curCharisma;
public int curIntelligence;
public int curDexterity;
public int curPerception;
public int curResource;
public int XP;
public List<itemResource> resourceInv;
public infoPawn(string newFirstName, string newLastName, int newHP, int newLuck, int newStrength, int newCharisma, int newIntelligence, int newDexterity, int newPerception, int newXP){
firstName = newFirstName;
lastName = newLastName;
HP = newHP;
luck = newLuck;
strength = newStrength;
charisma = newCharisma;
intelligence = newIntelligence;
dexterity = newDexterity;
perception = newPerception;
curHP = newHP;
XP = newXP;
curLuck = luck;
curStrength = strength;
curCharisma = charisma;
curIntelligence = intelligence;
curDexterity = dexterity;
curPerception = perception;
//resourceInv.Add (new itemResource ("gold", 3));
resource = 96 + strength*2 + dexterity*2;
curResource = 0;
}
}
And here is objectPlayer:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class objectPlayer : MonoBehaviour {
public string firstName;
public string lastName;
public int i = 1;
public infoPawn pawn;
// Use this for initialization
void Start () {
pawn = new infoPawn(firstName, lastName, 100, 1, 1, 1, 1, 1, 1, 0);
pawn.resourceInv.Add (new itemResource ("gold", 3));
}
// Update is called once per frame
void Update () {
//storedTotal = storedFood + storedGold + storedGunPowder + storedIron + storedStone + storedWater + storedWood;
if(mouseMove.gather == true && Vector3.Distance(mouseMove.target.transform.position, this.transform.position) < 3){
Gather ();
// InvokeRepeating ("Gather", 0, .1f);
mouseMove.gather = false;
mouseMove.gathering = true;
}
//if(mouseMove.gathering == true && Vector3.Distance(mouseMove.target.transform.position, this.transform.position) > 3){
// CancelInvoke ();
//
// mouseMove.gathering = false;
}
public void Gather(){
int r = Random.Range (0,mouseMove.target.GetComponent<objectWorld>().item.resourceInv.Count -1);
int c = Random.Range (1, Mathf.Min ((mouseMove.target.GetComponent<objectWorld> ().item.resourceInv [r].quantity), 10));
moveResource (pawn.resourceInv, mouseMove.target.GetComponent<objectWorld> ().item.resourceInv, new itemResource(mouseMove.target.GetComponent<objectWorld> ().item.resourceInv[r].name,Random.Range(1, c)));
}
public void moveResource(List<itemResource> inv1, List<itemResource> inv2, itemResource item){
bool added = false;
foreach (itemResource f in inv1) {
if (f.name == item.name && f.quantity >= item.quantity) {
f.quantity = f.quantity - item.quantity;
foreach (itemResource g in inv2) {
if (g.name == item.name) {
g.quantity = g.quantity + item.quantity;
Debug.Log(g.quantity + " " + g.name);
added = true;
}
}
if (added == false) {
inv2.Add (item);
Debug.Log(inv2[inv2.Count -1].quantity + " " + inv2[inv2.Count -1].name);
added = true;
}
}
}
}
}
This is the item Script “itemResource”:
using UnityEngine;
using System.Collections;
public class itemResource : MonoBehaviour{
public string name;
//public int weight;
public int quantity;
public itemResource(string newName, int newQuantity){
name = newName;
quantity = newQuantity;
}
}
All 3 scripts are applied to each the gameObject.