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

Make targets appear / disappear

Discussion in 'Scripting' started by Realter, Feb 3, 2021.

  1. Realter

    Realter

    Joined:
    Oct 5, 2020
    Posts:
    9
    Hi,

    in my Unity project I have three dots (see picture attached) and I want them to appear / disappear alternatively.

    I assigned the value 1 for the direction of the right target and the value -1 for the direction of the left target and told them to appear or disappear in this way:

    Code (CSharp):
    1.  if (direction == 1)
    2. {
    3. RightTarget.SetActive(true);
    4. yield return new WaitForSeconds(Targetduration);
    5. RightTarget.SetActive(false);
    6. }
    7.  
    8. if (direction == -1)
    9. {
    10. LeftTarget.SetActive(true);
    11. yield return new WaitForSeconds(Targetduration);
    12. LeftTarget.SetActive(false);
    13. }
    I want to do the same thing with the upper target.
    If 1 and -1 stand for the right and the left dot which value do I have to give to the upper target?

    Thank you
     

    Attached Files:

    Last edited: Feb 3, 2021
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Well, I don't see a picture, but what I would do is create an array of the dots. Then using a coroutine, loop through the array to make each dot disappear or appear as needed as long as I needed. but that is assuming what you are doing is what I'm picturing.
     
  3. Realter

    Realter

    Joined:
    Oct 5, 2020
    Posts:
    9
    Sorry, now I've fixed with the picture.
    Thanks for your reply.

    I actually use a coroutine anyway what I can't uderstand is why the values 1 and -1 make right and left targets appear / disappear correctly while if I assign for example the value 2 for the upper target it doesn't show what I would like.

    Is there a rule to follow to assign the values for directions or are they given randomly?
    Do I miss something I don't know?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Is
    direction
    a float? Don't compare floating point numbers for equality because sometimes what looks like 1 or 2 isn't actually 1 or 2 due to floating point imprecision. You can compare with Mathf.Approximately() but to be confident, do your own subtractive difference check.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Well, it's your code...

    So the question is, how are you changing the value of direction? It may be best to show your full script instead of a snippet.