Search Unity

Resolved Hello today i have weird problem with my code...

Discussion in 'Getting Started' started by TswendziPacha, Dec 7, 2023.

  1. TswendziPacha

    TswendziPacha

    Joined:
    Dec 7, 2023
    Posts:
    14
    Assets\Flash light.cs(38,20): error CS1002: ; expected
    im not experinced, tottal begginer, and use notepad... And dont know how to fix it...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Flashlight : MonoBehaviour
    6. {
    7.     public bool isOn = false;
    8.     public GameObject lightSource;
    9.     public AudioSource clickSound;
    10.     public bool failSafe = false;
    11.     void Update()
    12.     {
    13.             if (Input.GetButtonDown("Fkey"))
    14.         {
    15.             if (isOn == false && failSafe == false)
    16.             {
    17.                 failSafe = true;
    18.                 lightSource.SetActive(true);
    19.                 clickSound.Play();
    20.                 isOn = true;
    21.                 StartCoroutine(FailSafe());
    22.             }
    23.             if (isOn = true    && failSafe == false)
    24.             {
    25.                 failSafe = true;
    26.                 lightSource.SetActive(false);
    27.                 clickSound.Play();
    28.                 isOn = false;
    29.                 StartCoroutine(FailSafe());
    30.             }
    31.         }
    32.     }
    33.     IEnumerator FailSafe()
    34.     {
    35.         yield return nwe WaitForSeconds(0.25f);
    36.         failSafe = false;
    37.     }
    38. }
     
    Last edited: Dec 7, 2023
  2. Landa-100

    Landa-100

    Joined:
    Sep 9, 2018
    Posts:
    72
    Line 23
    Code (CSharp):
    1.             if (isOn = true    && failSafe == false)
    What isOn = true?
    You should install Visual Studio Code instead of notepad:)
     
  3. TswendziPacha

    TswendziPacha

    Joined:
    Dec 7, 2023
    Posts:
    14
    its if flashlight is, off or on
    i actualy work on it(download visual studio)

    but dont know what, code or community" i bet not community "
     
    Last edited: Dec 7, 2023
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,812
    You use two == to make a comparison, and one = to assign a value.

    And you should definitely install visual studio. It's impossible to code without a proper code editor.
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    981
    A WHOLE NWE WORLD!!!.....

    ..and line 35 should be:

    Code (CSharp):
    1. yield return new WaitForSeconds(0.25f);
     
    spiney199 likes this.
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    981
    My Visual Studio Code wouldn't have spotted the error but I wonder if Visual Studio would..
     
  7. TswendziPacha

    TswendziPacha

    Joined:
    Dec 7, 2023
    Posts:
    14
    OH S*** i see what i did there THX... XD
     
  8. TswendziPacha

    TswendziPacha

    Joined:
    Dec 7, 2023
    Posts:
    14
    now works... welp im stupid
     
  9. TswendziPacha

    TswendziPacha

    Joined:
    Dec 7, 2023
    Posts:
    14
    DAMIT now i cant turn off flash light...
     
  10. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    981
    It looks like your fail-safe failed. A simpler way:

    Code (CSharp):
    1. using UnityEngine;
    2. public class FlashLight : MonoBehaviour
    3. {
    4.     public GameObject lightSource;
    5.     public AudioSource clickSound;
    6.     void Update()
    7.     {
    8.         if (Input.GetButtonDown("Fkey"))
    9.         {
    10.             lightSource.SetActive(!lightSource.activeSelf); // toggle the active state of the game object
    11.             clickSound.Play();
    12.         }
    13.     }
    14. }
     
  11. TswendziPacha

    TswendziPacha

    Joined:
    Dec 7, 2023
    Posts:
    14
    i will try this now
     
  12. TswendziPacha

    TswendziPacha

    Joined:
    Dec 7, 2023
    Posts:
    14
    Works EVEN better THX