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

Setting a function of a script off, with an other script

Discussion in 'Scripting' started by Barft, Jul 14, 2015.

  1. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Hi all, i have made a timer that count down, and i want to "freeze" the time with an other script, the problem is that i don't know how to make it. Can somebody help me with it? I'm btw a beginner so i can't do alot at coding.


    this is the script of the timer:

    Code (CSharp):
    1.  
    2. float timeRemaining = 5;
    3.         public Font f;
    4.         public GUIStyle henk;
    5.        
    6.        
    7.        
    8.         // Use this for initialization
    9.         void Start () {
    10.            
    11.         }
    12.        
    13.         // Update is called once per frame
    14.         void Update () {
    15.             timeRemaining -= Time.deltaTime;
    16.         }
    17.        
    18.         void OnGUI () {
    19.             if (timeRemaining > 0) {
    20.                 GUI.contentColor = Color.black;
    21.                 GUI.skin.font = f;
    22.             float scalex = Screen.width / 1280.0f;
    23.             float scaley = Screen.height / 720.0f;
    24.                 GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scalex, scaley, 1));
    25.                 GUI.Label(new Rect(10,10,50,20), "Time:" + timeRemaining.ToString(" 0"), henk);
    26.             }
    27.             else {
    28.                 Application.LoadLevel("failedLevel1");
    29.             }
    30.         }
     
  2. kemar

    kemar

    Joined:
    Nov 4, 2014
    Posts:
    15
    Hi,
    Use a boolean variable for stopping the countdown. Change the state of freeze to true if you want to stop the countdown

    Code (CSharp):
    1. float timeRemaining = 5;
    2.     public Font f;
    3.     public GUIStyle henk;
    4.  
    5.     public bool freeze; //Your boolean
    6.  
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if (freeze == false)
    18.         {
    19.             timeRemaining -= Time.deltaTime;
    20.  
    21.         }
    22.     }...
     
  3. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    hi, thanks for your reaction. But how do i connect this with an other script?
     
  4. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    For example that this is the other script:

    if(wpress == true)
    {
    //here setting the time freeze
    }
     
  5. kemar

    kemar

    Joined:
    Nov 4, 2014
    Posts:
    15
    You have different ways for changing value with another script.

    Take a look at this and that.
     
    Last edited: Jul 14, 2015
  6. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    alright, thanks
     
    kemar likes this.
  7. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    i got a question this is my timer script now:

    Code (CSharp):
    1.     float timeRemaining = 5;
    2.     public Font f;
    3.     public GUIStyle henk;
    4.     public bool parkour1;
    5.    
    6.    
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         if (parkour1 == false)
    15.         {
    16.             timeRemaining -= Time.deltaTime;
    17.            
    18.         }
    19.     }
    20.    
    21.     void OnGUI () {
    22.         if (timeRemaining > 0) {
    23.             GUI.contentColor = Color.black;
    24.             GUI.skin.font = f;
    25.             float scalex = Screen.width / 1280.0f;
    26.             float scaley = Screen.height / 720.0f;
    27.             GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scalex, scaley, 1));
    28.             GUI.Label(new Rect(10,10,50,20), "Time:" + timeRemaining.ToString(" 0"), henk);
    29.         }
    30.         else {
    31.             Application.LoadLevel("failedLevel1");
    32.         }
    33.     }
    and this my other script:

    Code (CSharp):
    1. public bool[] holeTrigger = new bool[6];
    2.    
    3.  
    4.     protected string currentLevel;
    5.     protected int worldIndex;
    6.     protected int levelIndex;
    7.  
    8.  
    9.     IEnumerator wait () {
    10.         timerLevel1.parkour1 = true;
    11.         UnlockLevels ();   //unlock next level funxtion
    12.         Application.LoadLevel("fLevel1");
    13.     }
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         //save the current level name
    19.         currentLevel = Application.loadedLevelName;
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         //move the player based on left and right arrow keys
    25.         transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*10f, 0, 0); //get input
    26.     }
    27.  
    28.    
    29.  
    30.     public void TriggerHole(int index) {
    31.         holeTrigger[index] = true; //Set the hole trigger to true
    32.        
    33.         //Check if all holes have been triggered
    34.         if (AllTriggered) {  
    35.             StartCoroutine("wait");
    36.         }
    37.     }
    38.    
    39.  
    40.     //This method will iterate through each of the triggers in the array and return false if any of them are not true.
    41.     public bool AllTriggered {
    42.         get{
    43.             bool b = true;
    44.             foreach(bool bX in holeTrigger){
    45.                 if(!bX)
    46.                     b = false;
    47.             }
    48.             return b;
    49.             ;}
    50.     }
    51.  
    52.     protected void  UnlockLevels (){
    53.         //set the playerprefs value of next level to 1 to unlock
    54.         for(int i = 0; i < LockLevel.worlds; i++){
    55.             for(int j = 1; j < LockLevel.levels; j++){              
    56.                 if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
    57.                     worldIndex  = (i+1);
    58.                     levelIndex  = (j+1);
    59.                     PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
    60.                 }
    61.             }
    62.         }
    63.  
    64.     }
    65.  
    But it gives the error: An object reference is requierd to acces non-static member 'timerLevel1.parkour1'
     
  8. kemar

    kemar

    Joined:
    Nov 4, 2014
    Posts:
    15
    Your variable ' timerLevel1 ' is undeclared and not assigned.

    If those scripts are on the same GameObject you can try something like this (i don't know the name of your classes):

    1) Classe TimerScript (no change)

    Code (CSharp):
    1.  
    2. public class TimerScript : MonoBehaviour
    3. {
    4.  
    5.     float timeRemaining = 5;
    6.     public Font f;
    7.     public GUIStyle henk;
    8.  
    9.     public bool parkour1;
    10.    
    11.    
    12.    
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.        
    22.  
    23.         if (parkour1 == false)
    24.         {
    25.             transform.Rotate((5f),(0),(0) * (speed * Time.deltaTime));
    26.             timeRemaining -= Time.deltaTime;
    27.  
    28.         }
    29.     }
    30.  
    31.     public float speed = 0.1f;
    32.     void OnMouseDown ()
    33.     {
    34.     }
    35.    
    36.  
    37.    
    38.     void OnGUI ()
    39.     {
    40.         if (timeRemaining > 0) {
    41.             GUI.contentColor = Color.black;
    42.             GUI.skin.font = f;
    43.             float scalex = Screen.width / 1280.0f;
    44.             float scaley = Screen.height / 720.0f;
    45.             GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scalex, scaley, 1));
    46.             GUI.Label(new Rect(10,10,50,20), "Time:" + timeRemaining.ToString(" 0"), henk);
    47.         }
    48.         else
    49.         {
    50.             //Application.LoadLevel("failedLevel1");
    51.         }
    52.     }
    53. }

    2)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OtherScriptForTimer : MonoBehaviour
    5. {
    6.  
    7.     public bool[] holeTrigger = new bool[6];
    8.    
    9.    
    10.     protected string currentLevel;
    11.     protected int worldIndex;
    12.     protected int levelIndex;
    13.  
    14.     protected TimerScript timerLevel1;  // HERE Declaration of your variable
    15.    
    16.     IEnumerator wait ()
    17.     {
    18.  
    19.         timerLevel1.parkour1 = true;
    20.         UnlockLevels ();   //unlock next level funxtion
    21.         Application.LoadLevel("fLevel1");
    22.     }
    23.    
    24.    
    25.     // Use this for initialization
    26.     void Start () {
    27.         //save the current level name
    28.         currentLevel = Application.loadedLevelName;
    29.         timerLevel1 = GetComponent<TimerScript>();   //HERE instantiate your variable
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update () {
    34.         //move the player based on left and right arrow keys
    35.         transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*10f, 0, 0); //get input
    36.     }
    37.    
    38.    
    39.    
    40.     public void TriggerHole(int index) {
    41.         holeTrigger[index] = true; //Set the hole trigger to true
    42.        
    43.         //Check if all holes have been triggered
    44.         if (AllTriggered) {
    45.             StartCoroutine("wait");
    46.         }
    47.     }
    48.    
    49.    
    50.     //This method will iterate through each of the triggers in the array and return false if any of them are not true.
    51.     public bool AllTriggered {
    52.         get{
    53.             bool b = true;
    54.             foreach(bool bX in holeTrigger){
    55.                 if(!bX)
    56.                     b = false;
    57.             }
    58.             return b;
    59.             ;}
    60.     }
    61.    
    62.     protected void  UnlockLevels (){
    63.         //set the playerprefs value of next level to 1 to unlock
    64.         for(int i = 0; i < LockLevel.worlds; i++){
    65.             for(int j = 1; j < LockLevel.levels; j++){            
    66.                 if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
    67.                     worldIndex  = (i+1);
    68.                     levelIndex  = (j+1);
    69.                     PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
    70.                 }
    71.             }
    72.         }
    73.        
    74.     }
    75.  
    76. }
    77.  
     
  9. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Now its give the error not all code paths return a value
     
  10. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Ow , i forgot a yield return new waitforseconds to add, but now i got a error that says: object refernce not set to an instance of an object, i'm gonna be sick with all these errors....

    Do you know the problem?
     
    Last edited: Jul 14, 2015