Getting Type Parameter Error for Pillar Spawning Script in Flappy Bird Unity clone

Viewed 33

I am trying to make a 2D flappy bird game on Unity 2021.3.3f1, Ive figured out how to make the bird move in and made player move script for that, now I am trying to do a pillar spawning script for the pipes and so far I have this, I am trying to use my List pillarPrefabs and I feel like the error is coming from that as the error is this:

Assets\Scripts\PillarSpawner.cs(39,9): error CS0311: The type 'System.Collections.Generic.List<UnityEngine.GameObject>' cannot be used as type parameter 'T' in the generic type or method 'Object.Instantiate(T, Vector3, Quaternion, Transform)'. There is no implicit reference conversion from 'System.Collections.Generic.List<UnityEngine.GameObject>' to 'UnityEngine.Object'.

Here is my CODE for the pillar spawn script so far, any help would be appreciated, thank you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PillarSpawner : MonoBehaviour
{
    [SerializeField]
    private List<GameObject> pillarPrefabs;  //A list of pillar object templates
    [SerializeField]
    private float spawnMinTime; //The minimum amount of time to wait before spawning a pillar
    [SerializeField]
    private float spawnMaxTime; //The maximum amount of time to wait before spawning a pillar


    private float nextSpawnTime; //The next time to spawn a pillar

    // Start is called before the first frame update
    void Start()
    {
        nextSpawnTime += Time.deltaTime;

        if(nextSpawnTime > spawnMinTime)
        {
            SpawnObject();
            nextSpawnTime = 0f;

        }
        //Initalize when you will spawn the first pillar here.

    }

    // Update is called once per frame
    void Update()
    {

        //Here we will want to check if it's time to spawn another pillar. 
        float offsetY = UnityEngine.Random.Range(-spawnMaxTime, spawnMaxTime);
        
        Vector3 pos = new Vector3(this.transform.posiution.x, this.transform.position.y + offsetY);
        
        Instantiate(pillarPrefabs, pos, Quaternion.identity, this.transform);
        //To spawn a pillar:
        //Randomly select a pillar template from the list
        //Use Instantiate to create an instance of that template in the game world.
        //Select the next time to spawn a pillar


    }
}
1 Answers

BugFinder is correct, you can't just try to instantiate an entire list, you need to select an object in the list.

Here is an example of what you can write for your Update function:

// Update is called once per frame
void Update()
{
  //Here we will want to check if it's time to spawn another pillar. 
  float offsetY = Random.Range(-spawnMaxTime, spawnMaxTime);

  //Gets a random number from 0 to the amount in your list
  int random = Random.Range(0, pillarPrefabs.Count);

  //Instantiate a random pillar from the list using the int above
  Instantiate(pillarPrefabs[random], pos, Quaternion.identity, this.transform);
  
  //And do antoher random for spawntime
  float randomSpawnTime = Random.Range(spawnMinTime, spawnMaxTime);
  
  nextSpawnTime = randomSpawnTime;      

}

I hope this is helpful.

Also, look into coroutines, there WaitForSeconds will be really helpful, and are a lot simpler than timers (at least in my opinion).

Related