Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 fschaar · Jun 30, 2012 at 07:55 AM · instantiateprefabvariablesassign

Variable not Assigned

Hi there,

I have a problem with a variable. Unity says its not assigned, but I have assigned in the inspector. In my opinion everything is correct. I don't see a mistake. I have tried everythng: reassigning the variable, renaming the variable - nothing works. Anyone an Idea? Here is my code - maybe there is the mistake:

 var cubePrefab : Transform;
 var numberOfCubes :int =10; //number of Cubes needed in this level
 
 function Start()
 {
     var cube = new Array();
     for(var i=0;i<numberOfCubes; i++)
     {
         Instantiate(cubePrefab, Vector3 (2*i, 0.5, 0), Quaternion.identity);
     }
     //cube[5].Transform.translate(0,2,0);
 }
Comment
Add comment · Show 6
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 whydoidoit · Jun 30, 2012 at 07:58 AM 0
Share

Are you assigning it to the default value in the script or to an instance of the script on a GameObject?

avatar image Coderdood · Jun 30, 2012 at 04:46 PM 0
Share

Is this line:

cube[5].Transform.translate(0,2,0);

The line that is throwing the error? If so - you never assign anything to the cube array. Change the code to:

var cube = new Array();

for(var i=0;i

{

cube.Push( Instantiate(cubePrefab, Vector3 (2*i, 0.5, 0), Quaternion.identity) );

}

avatar image fschaar · Jul 01, 2012 at 06:58 AM 0
Share

Answer 1: The script is attached to the camera (only) and I assigned the prefab to the cubePrefab variable in the Inspector.

Answer 2: no, that line is not throwing the error, because it is marked as a comment. I went a step back, that's why I made it a comment. The Instanciate(...); Line is throwing the error without taking the array into play.

avatar image aldonaletto · Jul 01, 2012 at 07:06 AM 0
Share

Which variable Unity says it's unassigned? A common reason for this error is a duplicate instance of the script assigned to some other object by mistake - you set the variables in the "official" instance, but the forgotten one keeps throwing errors.

avatar image hijinxbassist · Jul 01, 2012 at 07:07 AM 2
Share

Just a word of advice (not to step on any toes) Dont use Array(). If you are com$$anonymous$$g from AS or Javascript it is natural to want to use the old fashion array, in reality a builtin array(fixed size unity array) is the way to go. If you need to access to resizing you can use a List, i will show both below.

 //Fixed Size Transform Array (can only contain the declared type, any type you choose)
 var cube:Transform[]=new Transform[numberOfCubes];
 
 //LIST 
 //At top of script
 import System.Collections.Generic;
 
 //Start
 //Resizable(same as Array, but much faster!) 
 var cube:List.<Transform> = new List.<Transform>();
Show more comments

1 Reply

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

Answer by fschaar · Jul 06, 2012 at 06:36 AM

Ok, thanks for the Answers, I think I have a solution now!

Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The variable othertransform of Prefab has not been assigned 2 Answers

Scripts of instantiated objects 1 Answer

assign different values to prefabs on instantiation 2 Answers

How to have unique instances of script on multiple instances of prefab? 1 Answer

Cannot drag and assign other class to prefab 1 Answer


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