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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems with Referencing a Function from another Script

Discussion in 'Scripting' started by CapitalistSquid, Sep 6, 2015.

  1. CapitalistSquid

    CapitalistSquid

    Joined:
    Aug 30, 2015
    Posts:
    12
    I am relatively new to Unity, and I am attempting to make a 2.5d platformer where you can use a grappling hook to move. I am trying to make it so the grappling hook can only be activated while the cursor is touching a wall. Here is the script that controls the cursor's collision with a wall:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WallScript : MonoBehaviour {
    5.  
    6.     AttractedScript s;
    7.     void Start () {
    8.         s = GameObject.FindGameObjectWithTag ("Cursor").GetComponent<AttractedScript> ();
    9.     }
    10.    
    11.     void Update () {
    12.    
    13.     }
    14.  
    15.     void OnTriggerEnter(Collider other){
    16.         if (other.tag == "Cursor") {
    17.             Debug.Log ("1");
    18.             s.Change();
    19.         }
    20.     }
    21.    
    22.  
    23. }
    24.  
    and here is the script that controls player movement :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AttractedScript : MonoBehaviour {
    5.     public GameObject attractedTo;
    6.     public float strengthOfAttraction = 5.0f;
    7.     public Rigidbody rb;
    8.     bool detect;
    9.  
    10.     void Start () {
    11.         detect = false;
    12.     }
    13.    
    14. void Update () {
    15.         if (detect) {
    16.             if (Input.GetMouseButtonDown (0)) {
    17.                 rb.useGravity = false;
    18.                 Vector3 direction = attractedTo.transform.position - transform.position;
    19.                 rb.AddForce (strengthOfAttraction * direction * 20);
    20.             }
    21.         }
    22.         if(Input.GetMouseButtonUp(0)){
    23.             rb.useGravity = true;
    24.             detect = false;
    25.         }
    26.     }
    27.  
    28.     public void Change () {
    29.         Debug.Log ("2");
    30.         detect = true;
    31.     }
    32.  
    33.  
    34. }
    35.  
    For some reason the Change function is not activating, causing the grappling hook to never work. However, the OnTriggerEnter function is working. Do you have any idea why this might be?
    Thank you.
     
  2. CapitalistSquid

    CapitalistSquid

    Joined:
    Aug 30, 2015
    Posts:
    12
    I am also receiving an Error Message that says
    "NullReferenceException: Object reference not set to an instance of an object
    WallScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/WallScript.cs:20)"
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I suspect s isn't ever being set to anything.

    Can you explain your scene setup, what script is on what gameobject etc.
     
  4. CapitalistSquid

    CapitalistSquid

    Joined:
    Aug 30, 2015
    Posts:
    12
    The WallScript is on platforms that the player cannot pass through but can grapple onto.

    The AttractedScript is on the player, so that they can move towards to the cursor gameobject when MouseButton 0 is pressed, in order to create a grappling hook effect.The attractedTo gameobject is set to the cursor.
     
  5. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Like LeftyRighty said, sounds like "s" might not be assigning in your Start function. Put a quick Debug.Log(s) at the end of your Start function and see if you get a NULL or whatever.

    Actually, after checking the Unity docs... change your start function to read as:

    The GameObject.FindGameObjectWithTag seems like the wrong command.
     
  6. CapitalistSquid

    CapitalistSquid

    Joined:
    Aug 30, 2015
    Posts:
    12
    Lentaq, the Debug.Log(s) is coming out as null, as you predicted. I changed the GameObject.FindGameObjectWithTag to GameObject.FindWithTag. This did not change the result. Do you know why this is occurring?

    Thank you.
     
  7. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    The .FindWithTag thing was just me thinking maybe they made the other obsolete since I didn't see it in the docs, but I don't think they did.

    Truthfully, I don't. Is your "Cursor" object already created/present when you hit the play button and your script initializes? All I can figure offhand is that there's an issue with "Cursor" tag when your script starts up or your AttractedScript isn't attached or something. Double check and make sure your tag is set on the object and your script is properly attached. Beyond that, I don't know what's causing it.

    Other than that, you could try breaking your GameObject.FindGameObjectWithTag into a separate temporary gameobject variable and do a s = getcomponent on it in a second step, but I think what you have is supposed to work.
     
  8. CapitalistSquid

    CapitalistSquid

    Joined:
    Aug 30, 2015
    Posts:
    12
    After switching my GameObject.FindGameObjectWithTag to GameObject.GetComponent, I continue to have the same problem with the same error being shown, and still s = null. At this point, I'm not sure what to do in order to fix this problem.