Null prefabs?

Hey, folks.

Really need some help here! I’m just not sure where the problem is. I’m getting a “The prefab you want to instantiate is null” error on the line with instantiation, line 46. This code is supposed to select a random prefab from the array set by the variable DifficultyLevel. I’ve assigned every single one of these prefabs by dragging and dropping a prefab from the hierarchy, and I’ve checked to make sure it’s calculating randomplacard and DifficultyLevel correctly. These numbers are fine. It’s just producing a null whenever I ask it to find out what prefab is in that part of the array…

Please help! I promise you the key to the city if you do.




var randomx;
var ycoord = .7;
var randomz;
var howmanyplacards = 20;
var randomplacard;
var a_placard: Transform;
var b_placard: Transform;
var c_placard: Transform;
var newplacard;
var DifficultyLevel = 0;

var eyeofbeholder: Transform;
var destroyer;
destroyer = eyeofbeholder.GetComponent("Character Destroyer");

var a: Transform;
var b: Transform;
var c: Transform;
var d: Transform;
var e: Transform;
var f: Transform;
var g: Transform;
var h: Transform;
var i: Transform;
var j: Transform;
var k: Transform;
var l: Transform;
var m: Transform;
var n: Transform;
var o: Transform;
var p: Transform;
var q: Transform;
var r: Transform;
var s: Transform;
var t: Transform;
var u: Transform;
var v: Transform;
var w: Transform;
var x: Transform;
var y: Transform;
var z: Transform;

var apple: Transform;
var kiwi: Transform;
var fig: Transform;
var pear: Transform;

var banana: Transform;
var cherry: Transform;
var grapes: Transform;
var lychee: Transform;

var strawberry: Transform;
var watermelon: Transform;
var pomegranate: Transform;
var pineapple: Transform;

var alphabet = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z];
var easy_fruits = [apple,kiwi,fig, pear];
var medium_fruits = [banana, cherry, grapes, lychee];
var difficult_fruits = [strawberry, watermelon, pomegranate,pineapple];
var alphabet_and_fruits = [apple,kiwi,fig, pear, banana, cherry, grapes, lychee, strawberry, watermelon, pomegranate,pineapple, a, b, c, d, e, f, g, h, i, c, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z];
var everything = [alphabet, easy_fruits, medium_fruits, difficult_fruits,alphabet_and_fruits];

function SetNewPlacard() 
	randomplacard = Mathf.Floor((Random.value) * everything[DifficultyLevel].length);
	randomx = (Random.value * 30) -15;
	randomz = (Random.value * 300)-700;
	newplacard = Instantiate (everything[DifficultyLevel][randomplacard], Vector3(randomx, ycoord, eyeofbeholder.transform.position.z+40),  Quaternion.identity);
	newplacard.transform.Rotate(0, 180, 0);
	newplacard.tag = "Ground Placards";

var Level_From_GameData;

function SetDifficultyLevel(Level_From_GameData:int)
	DifficultyLevel = Level_From_GameData;

only this line is wierd for me.

randomplacard = Mathf.Floor((Random.value) * everything[DifficultyLevel].length);

Random.value is “1” inclusive. so if you get 1 “randomplacard” will be set to the lenght of array and it will be NULL

just set it to “everything[DifficultyLevel].length-1” and try


if you want to do it dynamicly you can use Resource.Load for Instantiation of prefabs.

var instance : GameObject = Instantiate(Resources.Load("enemy"));