Search Unity

Save time problem (will pay if you can fix it!)

Discussion in 'Scripting' started by andro321111, Nov 24, 2015.

  1. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    I have this error: Type `Timer' does not contain a definition for `SaveTime' and no extension method `SaveTime' of type `Timer' could be found (are you missing a using directive or an assembly reference?)

    problem is that when i go to from lvl 1 to finish and scene change to lvl 2 time restarts but i want it keep going.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.     public float moveSpeed;
    6.     public GameObject ParticleSystem;
    7.     public Rigidbody rb;
    8.     private Vector3 input;
    9.     private float maxSpeed = 7f;
    10.     public Vector3 spawnLocation;
    11.     public Timer timer;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.  
    16. //        pozicija kocke, kjer bo začela.
    17.         spawnLocation = new Vector3(0,1,-13);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.  
    24.         input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis ("Vertical"));
    25.         if (rb.velocity.magnitude < maxSpeed)
    26.         {
    27.             rb.AddForce (input * moveSpeed);
    28.         }
    29.  
    30.         if (transform.position.y < -2)
    31.         {
    32.             Die ();
    33.         }
    34.     }
    35.     void OnCollisionEnter(Collision other)
    36.     {
    37.      
    38.         if (other.transform.tag == "Enemy") {
    39.             Instantiate(ParticleSystem, transform.position, Quaternion.identity);
    40.             Die ();
    41.         }
    42.     }
    43.  
    44.     void OnTriggerEnter(Collider other)
    45.     {
    46.  
    47.         if (other.transform.tag == "Finish")
    48.         {  
    49.             timer.SaveTime();  <----- Error here
    50.             Manager.CompleteLevel();
    51.  
    52.         }
    53.     }
    54.  
    55.     void Die()
    56.     {
    57.         transform.position = spawnLocation;
    58.     }
    59.  
     

    Attached Files:

    • 1.PNG
      1.PNG
      File size:
      4.5 KB
      Views:
      702
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    does your class, Timer, has a function that's called SaveTime?
    please post your Timer script.
     
  3. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    Code (CSharp):
    1.  public class Timer : MonoBehaviour
    2.   {
    3.      
    4.       private Text counterText;
    5.       private SpecialTimer timer;
    6.      
    7.       void Awake()
    8.       {
    9.           this.timer = new SpecialTimer();
    10.           this.counterText = GetComponent<Text>();
    11.       }
    12.      
    13.       void Update(){
    14.           this.timer.AddTime(Time.deltaTime);
    15.           this.counterText.text = this.timer.Minutes.ToString ("00") + ":" + this.timer.Seconds.ToString ("00");
    16.       }
    17.      
    18.       // if you want to restart timer without reload the game
    19.       public void RestartTimer()
    20.       {
    21.           this.timer.Restart();
    22.       }
    23.   }
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I don't see any function called SaveTime() so that's what unity is complaining about.
    maybe it's in SpecialTimer?
    then you should use timer.timer.SaveTime()
     
  5. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    Code (CSharp):
    1. public class SpecialTimer
    2. {
    3.      public float Seconds {get; private set;}
    4.      public float Minutes {get; private set;}
    5.    
    6.      public SpecialTimer()
    7.      {
    8.         this.Seconds = 0;
    9.         this.Minutes = 0;
    10.      }
    11.    
    12.      public void AddTime(float delta)
    13.      {
    14.         this.Seconds += delta;
    15.        
    16.          if(this.Seconds >= 60)
    17.          {
    18.             this.Seconds -= 60;
    19.             this.Minutes += 1;
    20.          }
    21.      }
    22.    
    23.      public void Restart()
    24.      {
    25.         this.Seconds = 0;
    26.         this.Minutes = 0;
    27.      }
    28. }
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    nope so the function SaveTime() just doesn't exist :)
    if you don't want it to reset when loading a scene, don't call on RestartTimer() and put DontDestroyOnLoad in void Awake()
     
  7. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    do you know how to make that function ? please if u know tell me im searching this for 2 days now and cannot fix it
     
  8. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    error CS0122: `Timer.timer' is inaccessible due to its protection level.

    why this error ?
     
    Last edited: Nov 24, 2015
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The inaccessible error means you need to put public in front of the member.

    As to saving there is a good video on persistence in the learn section.
     
  10. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    yeah i fixed it but now i still get error: Type `SpecialTimer' does not contain a definition for `SaveTime' and no extension method `SaveTime' of type `SpecialTimer' could be found (are you missing a using directive or an assembly reference?)

    please help me :(
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You haven't written a method called SaveTime. So one does not exist.
     
    Kerith likes this.
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    To be even more explicit the error is saying you need to do this

    Code (CSharp):
    1. public class SpecialTimer {
    2.      ...
    3.     public void SaveTime () {
    4.         // Some code in here
    5.     }
    6.      ...
    7. }
     
    jister likes this.
  13. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    NullReferenceException: Object reference not set to an instance of an object
    Movement.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Movement.cs:50)
     
  14. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    You have to add your timer to a gameObject using DontDestroyOnLoad(), as @jister stated.

    Your nullReferenceException, in case you didn't alter your script when you first posted it, is either one of those two methods:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.     if (other.transform.tag == "Finish")
    4.     {
    5.         timer.SaveTime();                     //line 49.
    6.         Manager.CompleteLevel();      //line 50.
    7.     }
    8. }
    One of those two got null'd, probably when you swap scenes since on 1st level everything works normal.

    In my opinion, it is time for learning section:
    https://unity3d.com/learn/tutorials

    There are plenty of great Unity projects that you can download free from Asset store. Instead of paying others, use that money to buy some books or download a complete project to play and learn from it.

    Best of luck!
     
  15. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    thanks for reply.. can i just ask you how can i figure it out what is the problem line 49 or line 50 ?

    Edit: i think its line 49
     
  16. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    You can figure it out like this:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.     if (other.transform.tag == "Finish")
    4.     {
    5.         if (timer != null)
    6.         {
    7.             timer.SaveTime();
    8.         }
    9.         else
    10.         {
    11.             Debug.LogError("Timer not found!");
    12.         }
    13.      
    14.         if (Manager != null)
    15.         {
    16.             Manager.CompleteLevel();
    17.         }
    18.         else
    19.         {
    20.             Debug.LogError("Manager not found!");
    21.         }
    22.     }
    23. }
    Check for output log. If you have Timer not found errors popping, you will have to assign Timer properly after reloading scene. If you haven't already, create this small script:
    Code (CSharp):
    1. public class DoNotDestroyBehaviour : MonoBehaviour
    2. {
    3.      void Start()
    4.      {
    5.           DontDestroyOnLoad( gameObject );
    6.      }
    7. }
    and add it to your gameObject where you added your Timer script. It should persist throughout scene swapping.
     
  17. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    cannot test your script for debug..

    error: Assets/Scripts/Movement.cs(54,29): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

    Edit: i fixed it and it says Timer not found!
     
    Last edited: Nov 25, 2015