- Home /
Question by
Reiji92 · Aug 17, 2016 at 04:35 PM ·
objectreferenceobject referencefunction callscripterror
Calling a function from an other script
So I want to call the "pickUp()" function from this script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class zombieLoot : MonoBehaviour {
int randomNumber;
Inventory inventory;
void Start () {
inventory = GetComponent<Inventory> ();
}
void Update(){
if (Input.GetButtonDown("F")) {
pickUp();
}
}
public void pickUp(){
AddItem(1);
}
}
I want to call it here:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class chase : MonoBehaviour {
zombieLoot zombieloot;
public Slider healthbar;
public Transform player;
static Animator anim;
public float speed = 0.02f;
void Start(){
anim = GetComponent<Animator> ();
zombieloot = GetComponent<zombieLoot> ();
}
void Update(){
if (healthbar.value <= 0 && anim.GetBool("isDead")) {
anim.SetBool ("isAttacking", false);
zombieloot.pickUp ();
}
}
}
and it says: NullReferenceException: Object reference not set to an instance of an object
why? In my first script I'm calling the Inventory scripts AddItem function, and it works! I want to call this pickUp function EXACTLY like the AddItem function, and it throws this error. It sees the script, but I don't know what could be wrong here. Help please!
Comment
Found the solution... I had to write
zombieloot = GameObject.Find ("Inventory").GetComponent<zombieLoot> ();
ins$$anonymous$$d of
zombieloot = GetComponent<zombieLoot> ();
Now I only have to somehow break out of the Update loop...:D hmh.