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

'GameObject' is a type but is used like a variable??

Discussion in 'Scripting' started by Gibonen, Oct 4, 2020.

  1. Gibonen

    Gibonen

    Joined:
    Oct 4, 2020
    Posts:
    6
    Hello i am stuck again with this simple code, there is error:
    Assets\shooting.cs(25,26): error CS0103: The name 'bullet' does not exist in the current context
    and
    Assets\shooting.cs(24,9): error CS0118: 'GameObject' is a type but is used like a variable
    please help ;)
    Capture.PNG
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    In line 24 you just say "GameObject =" you are missing your name; 'GameObject gameObject ='. But then it would only exist locally and your reference would be gone once the function ends. Declare GameObject gameObject in the class outside of a function then set 'gameObject ='.
    Line 25 just says bullet and I see no reference to a bullet.
     
    Gibonen likes this.
  3. Gibonen

    Gibonen

    Joined:
    Oct 4, 2020
    Posts:
    6
    Ok but i am trying to learn form yt and i just copied everthing what he does so why it is not working for me and working for him?:p
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Gibonen

    "Ok but i am trying to learn form yt and i just copied everthing what he does so why it is not working for me and working for him?"


    You haven't copied the code completely... in your code:
    GameObject = Instantiate...
    (like already mentioned by @adehm) and in the tutorial video it says:
    GameObject bullet = Instantiate(bulletPrefab...


    You can't have a type alone like that, as in the tutorial GameObject bullet means author creates a variable named "bullet" that is of type GameObject and then he Instantiates a prefab and it then gets assigned into this variable.
     
    Gibonen likes this.
  5. Gibonen

    Gibonen

    Joined:
    Oct 4, 2020
    Posts:
    6
    Thank you guys, that was so simple, idk why i didnt see that ;P