I have a script which instantiates a ground plane and puts random objs on it, in a somewhat random location. The prefabs are attached to a public variable and dragged and dropped on in the inspector. They work in the first script but when I try to call this method from another script I get “The prefab you want to instantiate is Null”.
searching around I have found every answer to the “prefab null” to be load.resources(). And this does work, but it doesn’t help me if I want to be able to switch around prefabs or learn what I am doing wrong in trying to call the other script (and it seems to be possible according to the other posts, just no explanation).
the error is on line 26 in RandObjs (the first code), though I think its the way I’m calling the method in the 2nd code at line 20.
any help would be greatly appreciated.
public class RandObjs : MonoBehaviour {
public GameObject[] obstacleArray;
public GameObject buildingPF;
// Use this for initialization
void Start () {
//GameObject building = Instantiate(buildingPF, new Vector3(13.5f, 0f, 3.5f), Quaternion.identity) as GameObject;
//Build(3.5f, 16, 39);
Build(28.5f, 16, 39); // +25, for sizing reasons
Build(53.5f, 41, 64);
Build(78.5f, 66, 89);
}
public void Build (float buildingZ, int forIEquals, int forILimit){
int objHorizontal1, objHorizontal2, objHorizontal3, x;
float timeDelta = Time.deltaTime * 50;
//each building and each verticle placemet ( i ) is 25units apart. prolly something +25 for a loop on creation to cut down code clutter.
// also make it so that this code snippet can be called from buildingScr when a building is destroyed.
//a return for the verticle positions
GameObject building1 = Instantiate(buildingPF, new Vector3(13.5f, 0f, buildingZ), Quaternion.identity) as GameObject;
building1.rigidbody.velocity = new Vector3(0, 0, -timeDelta);
for (int i = forIEquals; i <= forILimit; i+=8){
objHorizontal1 = Random.Range(1,6);
objHorizontal2 = Random.Range(1,6);
objHorizontal3 = Random.Range(1,6);
// if to check and not build ontop
if ((objHorizontal1 != objHorizontal2) & (objHorizontal1 != objHorizontal3)){
//create 1 unless it is equal to others
print (objHorizontal1 +"11");
x = Random.Range(0,7);
GameObject obstacle1 = Instantiate(obstacleArray[x], new Vector3(objHorizontal1*4.5f, 0, i), Quaternion.identity) as GameObject;
obstacle1.transform.parent = building1.transform;
}
if (objHorizontal2 != objHorizontal3){
//create 2 unless it is = to 3
print (objHorizontal2 + "22");
x = Random.Range(0,7);
GameObject obstacle2 = Instantiate(obstacleArray[x], new Vector3(objHorizontal2*4.5f, 0, i), Quaternion.identity) as GameObject;
obstacle2.transform.parent = building1.transform;
}
// 3 is free to create
print (objHorizontal3 + "33");
x = Random.Range(0,7);
GameObject obstacle3 = Instantiate(obstacleArray[x], new Vector3(objHorizontal3*4.5f, 0, i), Quaternion.identity) as GameObject;
obstacle3.transform.parent = building1.transform;
}
}
}
my 2nd script.
public class BuildingScr : MonoBehaviour {
private Shredder3DScr shredder3d;
private Controls player;
public GameObject build;
// Use this for initialization
void OnCollisionEnter( Collision collision) {
shredder3d = collision.gameObject.GetComponent<Shredder3DScr>();
player = collision.gameObject.GetComponent<Controls>();
if (shredder3d){
Destroy(gameObject);
//creates the building
//RandObjs builder = GetComponent<RandObjs>();
RandObjs builder = gameObject.AddComponent<RandObjs>();
//builder.Build(78.5f, 66, 89);
}
if (player){
print ("lost");
}
}