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

Time.time and line rendering code not working

Discussion in 'Scripting' started by Leyza, Nov 1, 2019.

  1. Leyza

    Leyza

    Joined:
    Sep 19, 2019
    Posts:
    9
    I'm trying to script 2 line renderers, one that tracks the player, and another that damages the player. Line1 tracks the player for a few seconds, then Line2 renders and shoots in the direction that Line1 originally was.
    Code (CSharp):
    1. public int timer1 = 5; //How long tracking line lasts
    2. public int timer2 = 5; //How long damage line lasts
    3. public int pause;
    4. public int increment;
    5. //There are other variables here that are used to assign line renderers
    6.  
    7. void Start()
    8.      {
    9.      //There's code here to assign line renderers. line1 renders the tracking line, line2 renders the damage line.
    10.  
    11.      pause = timer1 + timer2;
    12.      increment = pause;
    13.      line1.enabled = true;
    14.      line2.enabled = false;
    15.      }
    16.  
    17. void Update()
    18.      {
    19.      //There's code here to draw the lines using raycast.
    20.  
    21.      if (Timer.time >= timer1)
    22.           {
    23.           line1.enabled = false;
    24.           line2.enabled = true;
    25.           timer1 += increment;
    26.           }
    27.  
    28.      if (Timer.time >= pause)
    29.           {
    30.           line1.enabled = true;
    31.           line2.enabled = false;
    32.           pause += increment;
    33.           }
    34.      }
    Everything was working, but then I changed timer2 = 1;. The lines both still last 5 seconds. When I try to get rid of the timer1/timer2/etc variables and use their corresponding numbers in the variable's place, then damage line doesn't even render anymore. Then I clicked undo up until the point where I only changed timer2 = 1;, suddenly everything works again AND the damage line lasts 1 second as its supposed to, even though the code is exactly the same as before. Now if I change timer2 again, the same problem occurs. Why does this happen? Where is the problem?
     
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    Did you also check your edditor settings if you only change the code while its attached to a gameobject the gameobject will keep the old values.
     
    Leyza likes this.
  3. Leyza

    Leyza

    Joined:
    Sep 19, 2019
    Posts:
    9
    lol me. Thankyou.