Reputation: 166
I'm getting an error and I don't know why. From the error I can see that there is something wrong with line 12, I think.
The script is larger but is not needed to solve the problem. But if you really need it, you can ask for it.
Here is the code:
if(RandomInt==2) {
var randomNumberB = Random.Range(3,5);
for(var b = 0; b < randomNumberB; b++) {
var xCoB = childVector3.x + Random.Range(0,10);
var zCoB = childVector3.z + Random.Range(0,10);
var randomRotationB = Quaternion.Euler(0,Random.Range(0,360),0);
var chancheB = Random.Range(0,2);
if(chancheB == 0) {
var bushC = Instantiate(bushes[Random.Range(0,bushes.length)], Vector3(xCoB, childVector3.y, zCoB), randomRotationB);
bushC.transform.name = "bush";
} else {
Instantiate(cactus, Vector3(xCoB, childVector3.y, zCoB), randomRotationB).transform.name = "cactus";
}
}
}
Upvotes: 3
Views: 5827
Reputation: 81
it is easy if you have prefabs sitting in the root Resources folder. It is different if you want them to be in a subfolder of Resources. You will need to create a "Prefabs" folder within.
Since you are using Javascript
Given a prefab named "Your Prefab.prefab" within Assets/Resources:
var NewGameObject : GameObject = Resources.Load("Your Prefab"),Vector3.zero,Quaternion.identity);
This will allow you to have a reference to that prefab, loaded into memory.
If you wish to contain a prefab or more within a folder in Assets/Resources you must do the following:
2a. Create a folder named "Prefabs" within Assets/Resources.
2b. Create your folder there (e.g. "Resources/Prefabs/Your Prefab Folder");
2c. Given a prefab named "Your Prefab.prefab":
var NewGameObject : GameObject = Resources.Load("Prefabs/Your SubFolder/Your Prefab"),Vector3.zero,Quaternion.identity);
Notes:
Don't forget to remove the ".prefab" extension.
The Unity manual states that you can load assets from Resources without supplying a path with folder names. Certainly in regards to prefabs (and javascript?) that does not seem to be true (Unity 4.6).
Upvotes: 1
Reputation: 7824
Make cactus
a prefab.
Then make a public variable:
public GameObject cactus;
Then drag and drop the prefab into the script variable via de Editor. Then you can instantiate.
if(cactus != null)
{
GameObject g = Instantiate(cactus, new Vector3(xCoB, childVector3.y, zCoB), randomRotation) as GameObject;
g.name = "cactus";
}
Upvotes: 4