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

Yet another person having issues accessing functions from another script

Discussion in 'Scripting' started by Grandhighdruid, Sep 9, 2015.

  1. Grandhighdruid

    Grandhighdruid

    Joined:
    Sep 7, 2015
    Posts:
    3
    I am having serious problems in one of my scripts, which is trying to use a function from another script within a function in the first script, in getting the script to do it. Here is the basics:

    public class GameTurn : MonoBehaviour
    (This is the script I am trying to do the accessing with.)


    public class CardManager : MonoBehaviour
    (This is the script that the function I am trying to use is located.)

    void CardPoints ()
    (This is the function that I am trying to use in the first script.)

    Any help and advice would be GREATLY appreciated.
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You need a reference to an object of your CardManager, then you can simply call the method on it.
    Code (csharp):
    1.  
    2. public class GameTurn : MonoBehaviour
    3. {
    4.     public CardManager cardManager;    // Assign this in the inspector.
    5.  
    6.     public void SomeMethod()
    7.     {
    8.         if (!cardManager)
    9.         {
    10.             Debug.LogError("You have not assigned anything to cardManager in the inspector!");
    11.         }
    12.         else
    13.         {
    14.             cardManager.CardPoints();    // Call the method on the object.
    15.         }
    16.     }
    17. }
    18.  
     
  3. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    GameObject.Find ("YOUR OBJECT NAME").GetComponent<THE SCRIPT YOU ARE TRYING TO REACH TO>().lightsAreOn // This will return if its true.. Or you can still write == true tho..