C#C
C#3y ago
alex_aom

Can't find a GameObject's transform & rotation in Unity?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class WaveSpawner : MonoBehaviour
{
    public Transform enemyPrefab;
    private float gametime;
    private float initialGametime = -2f;
    public int timeBetweenWaves = 1;
    public Text waveCountdownText;
    private int waveIndex = 0;
    private int wavesSpawned = 0;
    private float nextWaveSpawnTime;

    void Start()
    {
        gametime = initialGametime;
    }

    private void Update()
    {
        GameObject spawnPoint = GameObject.Find("SpawnPoint");
        nextWaveSpawnTime = Mathf.Floor(waveIndex) * timeBetweenWaves;

        if (gametime > nextWaveSpawnTime)
        {
            waveIndex++;
            if (waveIndex >= wavesSpawned)
            {
                StartCoroutine(SpawnWave(spawnPoint));
                wavesSpawned++;
            }
        }

        gametime += Time.deltaTime;
        waveCountdownText.text = Mathf.Round(Mathf.Abs(gametime)).ToString();
    }

    IEnumerator SpawnWave(GameObject spawnPoint)
    {
        for (int i = 0; i < waveIndex; i++)
        {
            SpawnEnemy(spawnPoint);
            yield return new WaitForSeconds(0.3f);
        }
    }

    void SpawnEnemy(GameObject spawnPoint)
    {
        Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
    }
}


Assets\Scripts\WaveSpawner.cs(51,45): error CS1061: 'GameObject' does not contain a definition for 'position' and no accessible extension method 'position' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
Was this page helpful?