Way to Call a function from other script C#

If what I want to call is something like,

public void Example (int someVariable)

How do I access this from other script?

Make an instance of the class and call using the instance. Otherwise use “static” to access without instance.

1 Like

The unity Learning site is a good way to get started.
This video tutorial will show you how to use the GetComponent to access other scripts and their functions and variabls.
https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent?playlist=17117

1 Like

I list a bunch of ways in this video. GetComponet is by far the most common, you should learn that first.

1 Like

UPDATE: I got it to work.
Changed HurtPlayer to

using UnityEngine;
using System.Collections;

public class HurtPlayer : MonoBehaviour {
    public int damage = 50;
    Player player;

    void Awake (){
        player = gameObject.GetComponent ("Player") as Player;

        if (player == null){
            player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player>();
        }
    }

    public void OnCollisionEnter2D(Collider2D other){
        if (other.tag == "Player"){
            player.DamagePlayer(damage);

        }
    }
}

Thank you I will look into all of these
I have been trying to use get component for some time, but it won’t work for the current problem. I used it so many times before

My scripts involved are below.

using UnityEngine;
using System.Collections;

public class HurtPlayer : MonoBehaviour {
    public int damage = 50;

    public void OnTriggerEnter2D(Collider2D other){
        Player player;

        player = gameObject.GetComponent ("Player") as Player;
        if (other.tag == "Player"){
//            GameMaster.KillPlayer (this);
            player.DamagePlayer (damage);
        }
    }
}

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    [System.Serializable]
    public class PlayerStats {
        public int Health = 100;

    }

    public PlayerStats playerStats = new PlayerStats();

    public int fallBoundary = -20;

    void Update () {
        if (transform.position.y <= fallBoundary)
            DamagePlayer(999);
    }

    public void DamagePlayer (int damage) {
        playerStats.Health -= damage;
        if (playerStats.Health <= 0)
            Debug.Log ("KILL PLAYER");
            GameMaster.KillPlayer(this);
    
    }
}

I keep getting these errors.

NullReferenceException: Object reference not set to an instance of an object
HurtPlayer.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/HurtPlayer.cs:13)

The HurtPlayer script is attached to the Enemy prefab and the colliders are triggers. I tried both as a trigger and not as a trigger.

The Player script is attached to the Player.

Anything involving strings adds a layer of obscurity to the code and a level of ignorance to the debugger. I see that you fixed it (it took me a second to see that, as you posted newer code on top instead of on bottom), but for future reference you should avoid the specific way that you’re using GetComponent and instead use “GetComponent()”. Not only does it return as the same type already (no need to explicitly cast it) but if there’s some problem with the class name you’ll know right away when it pops up with an error rather than allowing you to compile.

2 Likes