- Home /
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.
Cheers,
Simon
THE CODE:
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; print(randomplacard); print(DifficultyLevel); print(everything); print(everything[DifficultyLevel][randomplacard]); 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; print(DifficultyLevel); }
Do you get this error everytime or just sometimes? Could you post the output of your 4 print messages before the error message?
Answer by iggy · May 29, 2011 at 12:51 PM
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
EDIT:
if you want to do it dynamicly you can use Resource.Load for Instantiation of prefabs.
http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html
var instance : GameObject = Instantiate(Resources.Load("enemy"));
hmmm...good catch, I did not know that about Random.value, but I don't think that was it. I'm still getting the error, for some terrible reason.
i would suggest you remove this line also if you don't need it:
var Level_From_GameData;
and i don't know, maybe give us info what those prints are outputing
Thanks for sticking with it! I need that variable Level_From_GameData, it's declaring a variable that's being changed by another script. The print statements return as follows:
print(randomplacard); RETURNED (IN THIS CASE) 22
print(DifficultyLevel); RETURNED (IN THIS CASE) 0
print(everything); RETURNS UnityEngine.Transform[][]
print(everything[DifficultyLevel][randomplacard]); RETURNED NULL
Then I get the "ArgumentException: The prefab you want to instantiate is null." error.
Does that tell you anything?
Hopefully,
Simon
I dont' have much experience with UnityScript, especially not with this kind of array declaration.
One thing I can think of is that maybe your arrays don't use references to the variables, but ins$$anonymous$$d copy the value these variables contain at the time of initialization. However, at compile time, all your variables are null, only after that the Inspector values are applied (I think).
Try to declare your arrays globally, but initialize them in Start() ins$$anonymous$$d:
var alphabet;
function Start() {
alphabet = [a, b, c ...]
}
This is a long shot, though.
Wolfram that works :) gj
this is my example script that worked:
var a: Transform; var alphabet:Array;
function Start() { alphabet = [a]; }
function Update () { if (Input.Get$$anonymous$$eyDown ("space")) print(alphabet[0]); //prints: First Person Controller (UnityEngine.Transform) }
Your answer
Follow this Question
Related Questions
How to make On Click() work with prefabs? 1 Answer
Is it possible to prevent certain components from being used in AssetBundles? 0 Answers
Prefab cant istiantiate through recources.load? 1 Answer
Using a Master Control Script w/Instantiated prefabs & multiple scenes? 0 Answers
How to Instantiate a GameObject from a ScriptableObject piece of script? 0 Answers