Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem running a function from another script c# help

Discussion in 'Scripting' started by forkmor, Oct 1, 2014.

  1. forkmor

    forkmor

    Joined:
    Sep 30, 2014
    Posts:
    6
    Im trying to run a function Chop() from a separate script, the scripts are on different objects idk if that matters
    i keep getting the error
    NullReferenceException: Object reference not set to an instance of an object
    chopped.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Standard Assets/chopped.cs:20)


    Code (CSharp):
    1.  
    2. public class Chop_wood : MonoBehaviour {
    3.     public int inventory = 0;
    4.  
    5.  
    6.     void OnTriggerEnter (Collider col)
    7.     {
    8.         Debug.Log ("at the tree");
    9.         //Chop ();  
    10.     }
    11.  
    12.    
    13.     public void Chop()
    14.     {
    15.         inventory++;
    16.         Debug.Log ("i am chopping the wood");
    17.     }
    18.  
    19.     void Update ()
    20.     {
    21.    
    22.     }
    23. }
    24.  
    and here is the other script
    Code (CSharp):
    1. public class chopped : MonoBehaviour {
    2.     public Chop_wood chopWood;
    3.  
    4.     void Start()
    5.     {
    6.         chopWood = GetComponent<Chop_wood> ();
    7.     }
    8.  
    9.  
    10.     void OnTriggerEnter (Collider col)
    11.     {
    12.  
    13.         if (col.collider.name == "Pirate_Chopper")
    14.         {
    15.  
    16.             Debug.Log ("in the trigger");
    17.             chopWood.Chop();
    18.         }
    19.     }
    20.    
    21.     void OnTriggerStay (Collider other)
    22.     {
    23.            
    24.     }
    25.  
    26. }
    27.  
     
  2. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    since the scripts are on different objects, you need a reference to the other object in the script calling for the function. using GetComponent<Chop_wood>() just looks for an instance of the script Chop_wood on its own object.

    add something like public Transform otherObject; to the beginning of "chopped", and in the inspector, drop the reference to the object with "Chop_wood" in it.

    then you should be able to do otherObject.GetComponent<Chop_wood>().Chop();
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You can do this directly with the Chop_wood class - there's no need to go through the transform.

    If you remove the Start function with the GetComponent call, you can drag the Chop_wood-containing object into that slot on the "chopped"-containing object.
     
  4. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    oh yeah you could do public Chop_wood otherObjectsChopWoodScript; and just call the function from that
     
  5. forkmor

    forkmor

    Joined:
    Sep 30, 2014
    Posts:
    6
    thanks, it works, lol i realized my problem soon after posting