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

Question CS0246, unity Can't find GameObject

Discussion in 'Getting Started' started by xeon0379, Jul 11, 2023.

Thread Status:
Not open for further replies.
  1. xeon0379

    xeon0379

    Joined:
    Jul 11, 2023
    Posts:
    4
    can anyone help? i was following a tutorial and did everyting as said but it says: Assets\PipeSpawner.cs(3,12): error CS0246: ''The type or namespace name 'GameObject' could not be found (are you missing a using directive or an assembly reference?)''
    oh and here is the tutorial:


    this is the code
    Code (CSharp):
    1. public class PipeSpawnScript
    2. {
    3.     public GameObject Pipe;
    4.     public float spawnRate = 2;
    5.     private float timer = 0;
    6.     public float heightOffset = 10;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         spawnPipe();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (timer < spawnRate)
    18.         {
    19.             timer = timer + Time.deltaTime;
    20.         }
    21.         else
    22.         {
    23.             spawnPipe();
    24.             timer = 0;
    25.         }
    26.  
    27.     }
    28.  
    29.     void spawnPipe()
    30.     {
    31.         float lowestPoint = transform.position.y - heightOffset;
    32.         float highestPoint = transform.position.y + heightOffset;
    33.  
    34.         Instantiate(Pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
    35.     }
    36. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You're probably missing a
    using UnityEngine;
    at the top of your script.
     
    Yoreki likes this.
  3. xeon0379

    xeon0379

    Joined:
    Jul 11, 2023
    Posts:
    4
    tried it but it doesn't work, it keeps saying "error cs0103 the name 'Instantiate' does not exist in the current context''
    and the same thing goes for the name 'transform''
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PipeSpawnScript
    5. {
    6.     public GameObject Pipe;
    7.     public float spawnRate = 2;
    8.     private float timer = 0;
    9.     public float heightOffset = 10;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         spawnPipe();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (timer < spawnRate)
    21.         {
    22.             timer = timer + Time.deltaTime;
    23.         }
    24.         else
    25.         {
    26.             spawnPipe();
    27.             timer = 0;
    28.         }
    29.  
    30.     }
    31.  
    32.     void spawnPipe()
    33.     {
    34.         float lowestPoint = transform.position.y - heightOffset;
    35.         float highestPoint = transform.position.y + heightOffset;
    36.  
    37.         Instantiate(Pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
    38.     }
    39. }
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Look at Mark's PipeSpawnScript and compare it to yours. You're missing something very important:
    upload_2023-7-11_17-45-39.png

    If you follow a tutorial, follow it exactly.
     
    Yoreki likes this.
  5. xeon0379

    xeon0379

    Joined:
    Jul 11, 2023
    Posts:
    4
    thx but it still wont work:
    upload_2023-7-11_13-47-13.png
    any idea on how to fix it
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Your script asset name and the class name inside the script need to match exactly.

    Once again, please follow the tutorial properly. I won't be helping any more if you can't follow the tutorial.
     
    Yoreki likes this.
  7. xeon0379

    xeon0379

    Joined:
    Jul 11, 2023
    Posts:
    4
    omg thx i did not know that
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    The forum isn't really a great place to tell you how to follow a tutorial exactly when copying code. If you make a mistake copying tutorial code, you've simply not copied it correctly so you should be able to look again at the tutorial and try following what's been typed more closely. Asking others on a forum to check what mistake you've made is, TBH, pretty low effort.

    Writing code requires that you are 100% accurate so I would recommend just going back to the tutorial and carefully following the instructions it provides.

    Thanks.
     
    Ryiah likes this.
  9. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,955
    Closed.
    As noted above, this forum isn't for debugging. Check your spelling, read the errors that are provided and if you need to, go to the Learn section to learn the basics.
     
    Ryiah likes this.
Thread Status:
Not open for further replies.