- Home /
My random tree spawner isn't working
I'm trying to write a script that can spawn att random locations on a plane. the thing is, when i run my script, nothing happens.
using UnityEngine;
using System.Collections;
public class spawnScript : MonoBehaviour {
public GameObject Palm;
public Vector3 spawnPos;
// Use this for initialization
void Start () {
spawnPos = new Vector3 (13,0,13);
Instantiate (Palm,spawnPos,Quaternion.identity);
}
}
NOTE: right now the code is not writen to be in random positions, i just picked some position so i could see if it worked.
The script is a component of the prefab "palm". The other things i have in my scene is a cube(working as ground), a character controller and a point light.
Am i grouping wrong? Or am I missing something in my code?
Could someone please try to tell me what I'm doing wrong?
dumb question: the script is attached to something on the scene?
also, try this: Instantiate (Palm,spawnPos,Quaternion.identity) as GameObject;
Found the problem, the problem was my prefab and the spawner(and some $$anonymous$$or code errors). I created a cube and used as a spawner and that worked awesome! Thanks! I also forgot to create a prefab in the resource-folder. But after some googling and trial and error it finally worked :)
Answer by GameVortex · Feb 05, 2015 at 04:47 PM
This script is a component on the Prefab Palm, and you are trying to instantiate the prefab palm with this script? That would end badly. First of all the script as @PrisVas said needs to be in the scene, not just on a prefab in your Assets.
Second, the problem occurs then when you do have this active in the scene on the prefab you are trying to instantiate. It would instantiate the prefab, which would then create another one of this script because it is on that prefab, and then that script would instantiate another prefab which would create another one of this script and so on. It would eventually crash the program.
You need to have your spawner script on a Spawner Object, separate from the prefab you are trying to spawn. That should sort things out. I recommend you to try and follow a few more tutorials to learn the basics first, before doing a completely custom thing.
Your answer
Follow this Question
Related Questions
google maps 3d API lacks detail 0 Answers
Create map of the USA 0 Answers
Adding rivers to procedural generated 2D map 1 Answer
How do I make an interactive map on Unity? 0 Answers
Can I use predifined hexagons in generating random maps? 2 Answers