- Home /
Intantiate problems
I'm trying to generate random obstacles for a temple runner type game, the main problem though is the issue surrounding "temp".
I'm crazy new at scripting so anyhelp would be great. Here's the script so far.
using UnityEngine;
using System.Collections;
public class RockSpawnScript : MonoBehaviour {
public GameObject Rock_001;
public GameObject Rock_002;
public GameObject Rock_003;
public GameObject Rock_004;
public GameObject Rock_005;
float timeElapsed = 0;
float spawnCycle = 3f;
int chooser = Random.Range (1,5);
void Update () {
timeElapsed += Time.deltaTime;
if (timeElapsed > spawnCycle)
{
Vector3 pos = temp.transform.position;
switch (chooser)
{
case 1:
temp = (GameObject)Instantiate(Rock_001);
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
break;
case 2:
temp = (GameObject)Instantiate(Rock_002);
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
break;
case 3:
temp = (GameObject)Instantiate(Rock_003);
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
break;
case 4:
temp = (GameObject)Instantiate(Rock_004);
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
break;
case 5:
temp = (GameObject)Instantiate(Rock_005);
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
break;
default:
temp = (GameObject)Instantiate(Rock_001);
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
break;
}
}
}
}
perhaps you could elaborate on "the main problem though is the issue surrounding "temp"."
what is the problem?
some things to think about:
this line: int chooser = Random.Range (1,5);
gets executed exactly ONCE - that's probably not what you intended, right?
you'll be better off looking at lists/arrays to store your rock objects/prefabs - then ins$$anonymous$$d of an ugly switch (bonus points for not doing an even uglier if/else, although the generated code isn't that different... anyway, i digress) you can simply index the list/array with the random number to get the required object/prefab.
all that other code is duplicated and could just as easily be done after you've got the rock prefab/object.
something like:
temp = Instantiate(Rocks[Random.Range (1,5)]) as GameObject;
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
way less code, way fewer problems.
obviously, none of that answers the main problem...
Hey there, sorry for the lack of info, the issue was that temp wasn't an assigned local variable. I see my noobish scripting really shows eh?
i've attempted your solution to solve the problem and it seems to be working fine except that error still shows as you predicted. Here's the line of code, hope it comes out properly...
using UnityEngine;
using System.Collections;
public class RockSpawnScript_2 : $$anonymous$$onoBehaviour {
public GameObject[] rocks;
public GameObject Rock_001;
public GameObject Rock_002;
public GameObject Rock_003;
public GameObject Rock_004;
public GameObject Rock_005;
public GameObject Rock_006;
public GameObject Rock_007;
float timeElapsed = 0;
float spawnCycle = 5f;
int chooser = Random.Range (1,7);
void update ()
{
timeElapsed += Time.deltaTime;
if (timeElapsed > spawnCycle)
{
int[] array ={1, 2, 3, 4, 5, 6, 7};
int chooser= UnityEngine.Random.Range(0, array.Length);
GameObject temp;
Vector3 pos = temp.transform.position;
temp = Instantiate(rocks[Random.Range (1,7)]) as GameObject;
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(14, -14), pos.y, pos.z);
}
}
}
So yeah, it doesn't solve the problem but atleast its cleaner, the issue as stated is "Use of unassigned local variable 'temp'. Not entirely sure where i'm going wrong on this.
cheers for your time mate I know i'm not the easiest when explaining a problem! :)
It's because you call Vector3 pos on temp when it's just an unassigned gameobject, so it doesn't have a transform or anything yet.
try:
GameObject temp = Instantiate(rocks[Random.Range (1,7)]) as GameObject;
Vector3 pos = temp.transform.position;
That way you declare and assign at the same time so you don't need to do stuff like the:
GameObject temp;
etcetera first. :) It's a waste of space.
That definately solved the problem thankyou! :)
Though I have an issue arise, for some reason they simply are not spawning into the level. Not sure why the references are correct and so on, my hierarchy simply isn't cloning them in.
What could be the issue here?
using UnityEngine;
using System.Collections;
public class RockSpawnScript : $$anonymous$$onoBehaviour {
public GameObject[] rocks;
public GameObject Rock_001;
public GameObject Rock_002;
public GameObject Rock_003;
public GameObject Rock_004;
public GameObject Rock_005;
public GameObject Rock_006;
public GameObject Rock_007;
float timeElapsed = 3;
float spawnCycle = 5f;
int chooser = Random.Range (1,7);
void update ()
{
timeElapsed += Time.deltaTime;
if (timeElapsed > spawnCycle)
{
int[] array ={1, 2, 3, 4, 5, 6, 7};
int chooser= UnityEngine.Random.Range(0, array.Length);
GameObject temp = Instantiate (rocks[Random.Range (1,7)]) as GameObject;
Vector3 pos = temp.transform.position;
temp = Instantiate(rocks[Random.Range (1,7)]) as GameObject;
pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(14, -14), pos.y, pos.z);
}
}
}
As far as I'm looking they should be instantiating across the axis specified and in good variety now. ~But null.
Cheers guys!
Answer by Phantomized · Oct 21, 2014 at 01:02 PM
I took the liberty of cleaning your code up a bit. This should work, and I tell you what I changed after the //
using UnityEngine;
using System.Collections;
public class RockSpawnScript : MonoBehaviour {
public GameObject[] rocks;
//I deleted all the single rock gameobjects. You don't need them when you have
//an array called rocks where you can put them all inside in the editor
float timeElapsed = 0; //Changed this to 0 because this is your "counter" and it should start at 0
float spawnCycle = 5f;
void Update () //You wrote update with a lowercase u here. So that's why it didn't work. Always remember to Capitalize methods.
{
timeElapsed += Time.deltaTime;
if (timeElapsed > spawnCycle)
{
timeElapsed = 0; //Once it becomes true that timeElapsed is higher than our spawnCycle value, we reset it back to 0 so it can begin counting up again from 0.
GameObject temp = Instantiate (rocks[Random.Range (0,rocks.Length)]) as GameObject; //Tells it to find a random rock between 0(the first rock in the rocks array) and the total length of your array.
Vector3 pos = temp.transform.position;
temp.transform.position = new Vector3(Random.Range(14, -14), pos.y, pos.z);
}
}
}
This has solved it brilliantly, I worked out the "U" in update and 0 but was still stumped, you've just helped solve whats had me baffled for two weeks! Also thanks for the quick feedback and explaining i'll carry that forward to other scripts! :)
Thanks!