Search Unity

Way to Call a function from other script C#

Discussion in 'Scripting' started by enkryptikon, Jul 31, 2015.

  1. enkryptikon

    enkryptikon

    Joined:
    Jun 16, 2015
    Posts:
    2
    If what I want to call is something like,

    public void Example (int someVariable)

    How do I access this from other script?
     
  2. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    Make an instance of the class and call using the instance. Otherwise use "static" to access without instance.
     
    enkryptikon likes this.
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    enkryptikon likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I list a bunch of ways in this video. GetComponet is by far the most common, you should learn that first.

     
    enkryptikon likes this.
  5. enkryptikon

    enkryptikon

    Joined:
    Jun 16, 2015
    Posts:
    2
    UPDATE: I got it to work.
    Changed HurtPlayer to
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HurtPlayer : MonoBehaviour {
    5.     public int damage = 50;
    6.     Player player;
    7.  
    8.     void Awake (){
    9.         player = gameObject.GetComponent ("Player") as Player;
    10.  
    11.         if (player == null){
    12.             player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player>();
    13.         }
    14.     }
    15.  
    16.     public void OnCollisionEnter2D(Collider2D other){
    17.         if (other.tag == "Player"){
    18.             player.DamagePlayer(damage);
    19.  
    20.         }
    21.     }
    22. }
    23.  
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HurtPlayer : MonoBehaviour {
    5.     public int damage = 50;
    6.  
    7.     public void OnTriggerEnter2D(Collider2D other){
    8.         Player player;
    9.  
    10.         player = gameObject.GetComponent ("Player") as Player;
    11.         if (other.tag == "Player"){
    12. //            GameMaster.KillPlayer (this);
    13.             player.DamagePlayer (damage);
    14.         }
    15.     }
    16. }
    17.  
    18. using UnityEngine;
    19. using System.Collections;
    20.  
    21. public class Player : MonoBehaviour {
    22.  
    23.     [System.Serializable]
    24.     public class PlayerStats {
    25.         public int Health = 100;
    26.  
    27.     }
    28.  
    29.     public PlayerStats playerStats = new PlayerStats();
    30.  
    31.     public int fallBoundary = -20;
    32.  
    33.     void Update () {
    34.         if (transform.position.y <= fallBoundary)
    35.             DamagePlayer(999);
    36.     }
    37.  
    38.     public void DamagePlayer (int damage) {
    39.         playerStats.Health -= damage;
    40.         if (playerStats.Health <= 0)
    41.             Debug.Log ("KILL PLAYER");
    42.             GameMaster.KillPlayer(this);
    43.    
    44.     }
    45. }
    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.
     
    Last edited: Jul 31, 2015
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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<className>()". 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.
     
    enkryptikon and Kiwasi like this.