Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Catlard · May 29, 2011 at 12:43 PM · prefabsnullinstantiation

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); }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · May 29, 2011 at 01:02 PM 0
Share

Do you get this error everytime or just sometimes? Could you post the output of your 4 print messages before the error message?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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"));

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Catlard · May 29, 2011 at 12:58 PM 0
Share

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.

avatar image iggy · May 29, 2011 at 01:34 PM 0
Share

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

avatar image Catlard · May 29, 2011 at 02:02 PM 0
Share

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

avatar image Wolfram · May 29, 2011 at 02:26 PM 1
Share

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.

avatar image iggy · May 29, 2011 at 04:29 PM 0
Share

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) }

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges