- Home /
Spawning different objects
Hey i've got a problem with spawning randomly objects out of a prefab folder on my screen that is being replaced after it passed the player.
The names of the objects are: wall1, wall2, wall3.
This is the script I use for spawning in the update() but before i used the same in the start() and he don't want to change it in the update function.
randomWall = Random.Range (1, 4);
GameObject wallPrefab = Resources.Load ("Prefabs/Wall" + randomWall) as GameObject;
The problem is that he only spawns one kind of objects and he doesn't change it
the meaning is that it becomes an endless runner that random generate some before thought situations after each other.
edit:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WallManager : MonoBehaviour
{
public GameObject runnerRef;
public float distanceToRecycle = 20;
public int numberOfWalls = 4;
public int movement = -5;
public List<GameObject> wall = new List<GameObject>();
static int randomWall = Random.Range(1,4);
void Start ()
{
GameObject wallPrefab = Resources.Load ("Prefabs/Wall" + randomWall) as GameObject;
for (int i=0; i<numberOfWalls; i++)
{
GameObject b = Instantiate (wallPrefab, Vector3.zero, Quaternion.identity) as GameObject;
Vector3 tempPos = new Vector3();
if(wall.Count>0)
{
tempPos = wall[wall.Count-1].transform.position;
tempPos.x += b.transform.localScale.x*.5f*35;
}
b.transform.position = tempPos;
b.transform.parent = gameObject.transform;
b.transform.parent.localPosition = new Vector3(30,12,20);
wall.Add(b);
}
}
// Update is called once per frame
void Update () {
randomWall = Random.Range (1, 4);
Debug.Log (randomWall);
transform.Translate (new Vector2 (movement * Time.deltaTime, 0));
if (wall [0].transform.position.x + distanceToRecycle < runnerRef.transform.position.x)
{
GameObject b = wall[0];
wall.Remove(b);
GameObject wallPrefab = Resources.Load ("Prefabs/Wall" + Random.value * 2 + 1) as GameObject;
Vector3 tempPos = new Vector3();
if(wall.Count>0)
{
tempPos = wall[wall.Count-1].transform.position;
tempPos.x += b.transform.localScale.x*.5f*35;
}
b.transform.position = tempPos;
b.transform.parent = gameObject.transform;
//b.transform.parent.localPosition = new Vector3(30,12,20);
wall.Add(b);
}
}
}
There's so little code here that we'd be guessing at the problem. Please post the rest of the script. Note that Random.Range(1,4) will only produce the values 1,2,3.
yeah, as robertu stated its hard to tell with a snipet..
Not really sure about random range, but you could try
("Prefabs/Wall" + randomWall.ToString())
@ robertu yes i know that, i got also 3 walls i would like to use now.
and i just posted the whole script that should do in how far i was include the problem above
Answer by robertbu · May 09, 2014 at 03:59 PM
I believe your problem is here:
GameObject wallPrefab = Resources.Load ("Prefabs/Wall" + Random.value * 2 + 1) as GameObject;
Instead of using 'randomWall' as you do in Start(), you are using some other calculation. Note that Random.value will always be between 0 and 1.
well in the start() he gives it a value like wall2 and then in the update() it also don't change.
Also in my game itself i get 6 times wall2 then. I guess i have to change that and every time that the wall is far enough from the player it removes to put another random wall.
I only haven't really an ideal how i change the game object that it makes different ones.
Do this on the line before the Instantiate() in Update():
string st = "Prefabs/Wall" + Random.value * 2 + 1;
Debug.Log(st);
this doesn't work.
I'm just sure there is first a problem in the start function why he doesn't say there 3 walls but the same wall all the time. but i don't know what i have to change to make the difference.
What does 'this doesn't work' mean. I suggested these to lines of code to debug your problem...you could see what string was used and therefore what wall it was trying to get. So what did it say? And why are you not using Random.Range() like in Start()?
public List<GameObject> wall = new List<GameObject>();
Is only using one type of the wall and in the update() it doesn't change no matter what what i want it to do. Also is there no difference in the start(), it makes 6 walls of the same number even when i tried to put the randomWall = Random.Range (1, 4); in the for loop of the start().
It only takes a single object and use it for all the time.
First i was using random.range also in the update() but it didn't work, also my $$anonymous$$cher and partner in making the game said i should use random.value
If i know the solution of the start() problem i will probably to fix the update() with the same problem.
maybe the screenshot i added will help
Your answer
Follow this Question
Related Questions
Does using both Update() and a coroutine cause respawning problems? 0 Answers
Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers
Failed to start unity package manager. Version 2017.2 1 Answer
Question about Unity documentation GameObject.Find 2 Answers
Probably Dumb Question: Update(), Start(), etc. Don't Work Inside Classes (JavaScript)...? 2 Answers