Search Unity

Call a method in another MonoBehaviour script ?

Discussion in 'Scripting' started by Althos, Nov 17, 2019.

  1. Althos

    Althos

    Joined:
    Sep 23, 2019
    Posts:
    17
    Hi, I'm creating a card game and was trying to create a simple resource system : using text to display the mana, to which I attached a Monobehaviour script called "Cost", with a method that gets an int value as an input (which is the cost of any card dropped into a playzone) and substracts it from the total mana and then display it with a ToString() method.
    My problem is that to call that method from another script (let's say my "OnDrop" script which detects when a card is dropped onto the field), I need to instantiate an object of type Cost to call my method, but I have no idea how to do that, as I don't want to add the script to the object calling said method, but I also need my script to be MonoBehaviour to attach it to my display text.

    Is there a way I can do that, or should I just do it in a completely different way ?

    Thanks a lof for your help!
     
  2. Deleted User

    Deleted User

    Guest

  3. Althos

    Althos

    Joined:
    Sep 23, 2019
    Posts:
    17
    Thanks! That did fix my issue, although I feel I have not completely grasped how it works yet
     
  4. Deleted User

    Deleted User

    Guest

    Basically:
    1. you have script a and script b;
    2. in script b, you need to access a variable or a function that belongs to script a,
    3. in script b, you declare and define script a,
    4. in script a, you make the function or variable you need to access from script b "public",
    5. in script b, you use script _a.variable = new value, or script_a.Function() to change that variable in script a or use the function described in script a.
    The two following scripts, CameraController attached to the main camera and GameController attached to an empty game object named GameController will make the camera rotate around its y axis. :)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraController : MonoBehaviour
    4. {
    5.     private Camera mainCamera;
    6.  
    7.     private void Start()
    8.     {
    9.         Debug.Log(mainCamera);
    10.     }
    11.  
    12.     public void CameraRotate()
    13.     {
    14.         transform.Rotate(Vector3.up, Space.World);
    15.     }
    16. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GameController : MonoBehaviour
    4. {
    5.     private CameraController cameraController;
    6.  
    7.     private void Start()
    8.     {
    9.         cameraController = Camera.main.GetComponent<CameraController>();
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         cameraController.CameraRotate();
    15.     }
    16. }
     
    Last edited by a moderator: Nov 20, 2019
    BaconNicholas, rc82 and renceed like this.
  5. Althos

    Althos

    Joined:
    Sep 23, 2019
    Posts:
    17
    That's way more intuitive now! Thanks a lot for your help!