C
C#2y 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);
}
}
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?)
3 Replies
alex_aom
alex_aom2y ago
The GameObject I am doing .Find to get does not seem to have a .position or .rotation
Thinker
Thinker2y ago
Isn't it .transform.position?
alex_aom
alex_aom2y ago
Uh oh Thank you sir haha