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.

Question object spawns twice only

Discussion in 'Getting Started' started by calltweed812, Nov 28, 2022.

  1. calltweed812

    calltweed812

    Joined:
    Nov 28, 2022
    Posts:
    7
    hello, i've been quite stumped with this issue, i'm trying to create a simple flappy bird clone to learn unity and C#, and up until now, i've gotten by with only minor issues, i have been using the tutorial located at https://weeklyhow.com/flappy-bird-unity-tutorial and have got to the part where i tell the pipes to spawn for the bird to go through, however, when i try, only the first two pipes spawn and when they go off screen, additional pipes appear but are off screen as well.
    here is a preview of my issue:

    i did notice the tutorial is for unity version 2019.1.1F1, so this may be a issue. in addition, i have attached the code for the 3 scripts below.

    player.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     public float velocity = 2.4f;
    8.     private Rigidbody2D rigidbody;
    9.     public GameManager gameManager;
    10.     public bool isDead = false;
    11.  
    12.     private void OnCollisionEnter2D(Collision2D collision)
    13.     {
    14.         isDead = true;
    15.         gameManager.GameOver();
    16.     }
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         rigidbody = GetComponent<Rigidbody2D>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (Input.GetMouseButtonDown(0))
    28.         {
    29.             rigidbody.velocity = Vector2.up * velocity;
    30.         }
    31.     }
    32. }
    spawner.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class spawner : MonoBehaviour
    6. {
    7.     public float queueTime = 1.5F;
    8.     private float time = 0;
    9.     public GameObject Obstacle;
    10.  
    11.     public float height;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if (time > queueTime)
    17.         {
    18.             GameObject go = Instantiate(Obstacle);
    19.             go.transform.position = transform.position + new Vector3(0, Random.Range(-height, height), 0);
    20.  
    21.             time = 0;
    22.  
    23.             Destroy(go, 10);
    24.         }
    25.  
    26.         time += Time.deltaTime;
    27.     }
    28. }
    Obstacle.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Obstacle : MonoBehaviour
    6. {
    7.     public float speed;
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         transform.position += ((Vector3.left * speed) * Time.deltaTime);
    13.     }
    14. }
     
  2. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    126
    From your spawner.cs it seems to be instantiating the pipe, as you can see in your hierarchy (spawning Obstacle gameObjects - you can see this in hierarchy on the left at 14 seconds of the video), I image the problem is due to the position your spawning each new pipe. If you click play and look in your hierarchy of each Obstacle(Clone) and pause the game and check where each one is, you might find they are all stacked on top of each other. Also you're doing + new Vector3 for a 2D game, you should be able to just use a new Vector2.
     
  3. calltweed812

    calltweed812

    Joined:
    Nov 28, 2022
    Posts:
    7
    i just tried changing the vector from vector3 to 2, however, i get several errors including:

    NullReferenceException: Object reference not set to an instance of an object
    Unity.Cloud.Collaborate.UserInterface.CollaborateWindow.OnDisable () (at Library/PackageCache/com.unity.collab-proxy@1.5.7/Editor/Collaborate/UserInterface/CollaborateWindow.cs:86)
    NullReferenceException: Object reference not set to an instance of an object
    Player.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Player.cs:15)
    NullReferenceException: Object reference not set to an instance of an object
    Player.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Player.cs:15)
    NullReferenceException: Object reference not set to an instance of an object
    Player.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Player.cs:15)
    NullReferenceException: Object reference not set to an instance of an object
    Player.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Player.cs:15)
    Assets\spawner.cs(19,62): error CS1729: 'Vector2' does not contain a constructor that takes 3 arguments
    Assets\Obstacle.cs(12,9): error CS0034: Operator '+=' is ambiguous on operands of type 'Vector3' and 'Vector2'
     
  4. calltweed812

    calltweed812

    Joined:
    Nov 28, 2022
    Posts:
    7
    additionally, i had a look and it doesn't look like the pipes are being grouped together, it appears the pipe images are getting grouped (as in texture1 gets grouped to texture2)
     
  5. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    126
    It states what the error is there:
    Assets\spawner.cs(19,62): error CS1729: 'Vector2' does not contain a constructor that takes 3 arguments
    Vector2 takes 2 arguments.
     
  6. calltweed812

    calltweed812

    Joined:
    Nov 28, 2022
    Posts:
    7
    i removed the second zero in spawner.cs and now two more errors have appeared:


    Assets\Obstacle.cs(12,9): error CS0034: Operator '+=' is ambiguous on operands of type 'Vector3' and 'Vector2'
    Assets\spawner.cs(19,37): error CS0034: Operator '+' is ambiguous on operands of type 'Vector3' and 'Vector2'
     
  7. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    126
    It's because you're trying to smash together a Vector3 and Vector2.

    Can't you just do something like, this is what I was getting at:

    Code (CSharp):
    1.  go.transform.position = new Vector2(transform.position.x, Random.Range(-height, height));
    To be fair, you can set the position on the Instantiate call, so not sure why it's being done after it's instantiated.

    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    Take a look at the second argument. You'll also need to set Quaternion.identity (if there's no rotation) as the third argument, which relates to rotation.
     
  8. calltweed812

    calltweed812

    Joined:
    Nov 28, 2022
    Posts:
    7
    this fixed the second error, but i cannot figure out the way to get the first one to work, for reference, i will post the just the line below:
    Code (CSharp):
    1. transform.position += ((Vector2.left * speed) * Time.deltaTime);
    this is in obstacle.cs, by the way.
     
  9. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    126
    Do Debug.Log(transform.position); and Debug.Log(((Vector2.left * speed) * Time.deltaTime)) just above, you might be able to figure out why then
     
  10. calltweed812

    calltweed812

    Joined:
    Nov 28, 2022
    Posts:
    7
    i now get a new error:


    Assets\Obstacle.cs(12,15): error CS0117: 'Debug' does not contain a definition for 'log'
     
  11. calltweed812

    calltweed812

    Joined:
    Nov 28, 2022
    Posts:
    7
    nevermind, i forgot the capital letter. instead i now get:


    Assets\Obstacle.cs(12,9): error CS0131: The left-hand side of an assignment must be a variable, property or indexer
     
  12. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    126
    If you comment out everything except the Logs, I think you'll find one is a Vector3 and the other is a float value, and you're trying to += them together.
     
  13. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    802
    Definitely watch your spelling when you're typing code in, it's VERY case-sensitive and if you spell a word wrong it'll not thank you.