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. Dismiss Notice

how to call void method from nother class in if() statement in the second class?

Discussion in 'Scripting' started by victor2dgames, Apr 10, 2016.

  1. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    how to call void method from nother class in if() statement in the second class? thanks .
     
  2. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    easiest way is to create a public property and attach your script to it, then simply use this property to call public methods of that script from your current script that holds the property of the other script.

    Code (CSharp):
    1. class scriptA
    2. {
    3.     scriptB myAttachedScript;
    4.  
    5.     void Start()
    6.     {
    7.         myAttachedScript.CallMethodXY();
    8.     }
    9. }
    attach scriptB to scriptA in Unity Editor and you're done.
    (just typed this out of my head as example, you might change stuff here and there to make it work.)
     
    Kiwasi and victor2dgames like this.
  3. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    thanks , as you said , now i want to use method .StartGameTouchButton () from script InGamePause in Update method in script GameManager
    what i did is :
    Code (CSharp):
    1.     public InGamePause igp;
    2.  
    3. void Update () {
    4.  
    5.         if (!gameStarted && Time.timeScale == 0) {
    6.  
    7.   if(          igp.StartGameTouchButton ();)
    8.  
    9.             timeManager.ManipulateTime (1, 1f);
    10.             ResetGame ();
    11.         }
    but there it does not work . can you help me more? thanks .
     
  4. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    try to format your code better, so it's easier to read for others and they can faster help you ;)

    what exactly doesn't work? is the timeManager also an attached script or a global class? does your StartGameTouchButton function return an boolean? ...

    just describe us more what happens, and what doesn't work so we can think about some solutions and fixes..
     
    victor2dgames likes this.
  5. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    SlyRipper , as you said timeManager is also an attached script .
    StartGameTouchButton function returns no boolean . with these 2 notes i fixed the problems , but this is another question :

    how in the following method i change time.time scale to 1 after 3 seconds?thanks .
    Code (CSharp):
    1.     public void CancelButton()
    2.     {
    3.         Time.timeScale = 0;
    4.  
    5.         InGamePausePanel.SetActive (false);
    6.  
    7.         InGamePauseButton.enabled = true;
    8.  
    9.  
    10.     }
     
    Last edited: Apr 11, 2016
  6. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    victor2dgames likes this.