• 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 AyJayKaySoft · Oct 07, 2013 at 05:51 PM · variablenameidentifierunknown

set up identifier name on the fly

Hi there, if I am using an name of a variable that isn't set up so far, I will get the unknown identifier error. That makes sence to me. But if I am sure the name will be set befor it is used, I can't tell the compiler ;) So, if I need to set up on the fly without the same class (special array length or dimension...) it's not possible:

 function SetUpIdentifier (){
     var bla : SomethingSpecial;
 }
 function Start (){
     SetUpIdentifier();
     bla.SomethingVerySpecial();
 }

or even

 function DoSomething (){
     bla.SomethingVerySpecial();
 }
 function Start (){
     var bla : SomethingSpecial;
     DoSomething();
 }

Is it possible to change an variable or to use a so far unset variable... or something else to solve this problem? Thanks :)

Comment
Add comment · Show 5
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 ArkaneX · Oct 07, 2013 at 09:01 PM 0
Share

Why can't you use class scoped variables?

avatar image AyJayKaySoft · Nov 27, 2013 at 09:05 PM 0
Share

I'm not sure I understand what you mean :) Because this could even happen when you do some complex stuff outside more than one class... or did I get you wrong?

avatar image ArkaneX · Nov 27, 2013 at 09:47 PM 0
Share

I meant that you're declaring your variable in a scope of a function, so it is visible in that function only. When function exits, your variable is lost.

Ins$$anonymous$$d, you can declare a variable outside any function (in a class itself). This will make it available in all the functions of this class:

 var bla : SomethingSpecial;
 function SetUpIdentifier (){
     bla = new SomethingSpecial();
 }
 function Start (){
     SetUpIdentifier();
     bla.SomethingVerySpecial();
 }

So now it's my turn to ask if this is what you want... or did I get you wrong? ;)

avatar image AyJayKaySoft · Feb 03, 2014 at 02:28 PM 0
Share

Oh, that makes sense. Ok, than the right question should be like: How do I change the type of any variables, so that I am able to use my SomethingSpecial type? (the type will be build on the fly... like special dimensions of a special list/array or somthing someone smarter can imagine :D )

avatar image ArkaneX · Feb 03, 2014 at 02:47 PM 0
Share

It's not possible to change variable types. If a variable is declared as X, then it will always be X.

If you plan to build types on the fly, then I guess you want to use reflection. In this case you can declare your variable as object, and use reflection to access methods/properties. But remember that reflection is slow.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 People are following this question.

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

Related Questions

Storing a variable and it's name 3 Answers

Call static variable dynamically 1 Answer

Problem naming instances with incrementally numbered counter 1 Answer

BCE0005: Unknown identifier: 'transform' 2 Answers

Is it bad to use long variable names? 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