Hi guys, im pretty new to c# and unity, so it may be a stupid question.
Basically i’m trying to Instantiate 3 (or a variable amount) of game object from a prefab (wich is bound in unity onto script on other game object) somewhere “on the edge of the screen”. However, when i try to acces the newObject to change its name i get that error mentioned in the title.
using System;
using UnityEngine;
namespace Assets.Code.States
{
public class Generator
{
private References refInst;
private Vector3 goThere;
private Transform objectPrefab;
public Generator ()
{
}
public void CreateObjects()
{
refInst = GameObject.Find ("GameManager").GetComponent<References>();
objectPrefab = refInst.bigObjectPrefab1;
int amount = 3;
for (int i = 0; i < amount; i++)
{
float whereDoIGo = UnityEngine.Random.value * 4;
if (whereDoIGo <= 1)
{
goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
}
if (whereDoIGo > 1 && whereDoIGo <= 2)
{
goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
}
if (whereDoIGo > 2 && whereDoIGo <= 3)
{
goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
}
if (whereDoIGo > 3 && whereDoIGo <= 4)
{
goThere = Camera.main.ViewportToWorldPoint (new Vector3(0.01f, UnityEngine.Random.value, 10));
}
GameObject objectNew = GameObject.Instantiate(objectPrefab, goThere, Quaternion.identity) as GameObject;
objectNew.name = "Hi there! " + i;
}
}
}
}