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 spacepilot · Dec 20, 2011 at 02:30 PM · variabletypedeclare

Redim a variable

How can I change the type of a variable in unity-js? I didn't find anything about it on the forums. If there is no way: Is it possible to "kill" a variable so I can re-declare it afterwards? I want to reuse the name but change the content. Thanks for help in advance.

Comment
Add comment · Show 10
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 FLASHDENMARK · Dec 20, 2011 at 02:36 PM 0
Share

I really don't think you can do that(I am sure someone will correct me, though). However variables used inside one function cannot be accessed in another, therefore you can re-declare it in another function.

However I am a little unsure whether you can reuse the same name, in different functions. I can't test that right now I am sorry.

avatar image spacepilot · Dec 20, 2011 at 06:05 PM 0
Share

What's about enum-vars? I found many examples of usage. But nobody seems to define the types of the sub-vars. Is it possible nevertheless?

avatar image dannyskim · Dec 20, 2011 at 09:16 PM 0
Share

what exactly are you trying to change about the variable? you can always try and typecast if it's within the same realm.

avatar image syclamoth · Dec 20, 2011 at 09:34 PM 2
Share

$$anonymous$$ore importantly, why? Why do you want to do something which is unequivocally dangerous?

avatar image spacepilot · Dec 21, 2011 at 06:02 AM 1
Share

I want to change the type. What is typecast and what is realm?

Show more comments

3 Replies

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

Answer by flamy · Dec 21, 2011 at 06:23 AM

Declare it as System.Object and u can cast it to any valid(serilizable) c# type! Object would refer to UnityEngine.Object, you should declare it a System.Object. you can jus assign what ever datatype to the variable of type object!! to get the current type use GetType fuction. For more info go this page.The snippet could be like this

 var obj:System.Object;
 
 obj=45;
 print(obj.GetType());
 
 
 obj=45.8f;
 print(obj.GetType());
 
 
 obj=true;
 print(obj.GetType());

You can check teh results urslef!! Hope it helps :)

Comment
Add comment · Show 12 · 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 spacepilot · Dec 21, 2011 at 06:37 AM 0
Share

That's it! Thank you.

avatar image spacepilot · Dec 21, 2011 at 06:39 AM 0
Share

One more question: When assigning

 comptype = new Transform();
the compiler says:

"BCE0120: 'UnityEngine.Transform.constructor()' is inaccessible due to its protection level"

All other var-types make no trouble.

avatar image flamy · Dec 21, 2011 at 06:45 AM 1
Share

that is simple

while assigning the transform do something like this

obj= transform as UnityEngine.Object;

this would do it!

avatar image spacepilot · Dec 21, 2011 at 06:50 AM 0
Share

It does !! [Edit:]...not! I now get the same error-message.

avatar image flamy · Dec 21, 2011 at 07:27 AM 0
Share

dude are u trying to use new Transform() or something like this?!?! it is not possible to do in unity!! because you cannot create transform, you can only access transform of a gameObject!... if u want to access the transform of the current object you can call like this

obj=transform as UnityEngine.Object;

if you want to access some other object you have to use transform.Find or a public variable and attach it from editor!!

Show more comments
avatar image
2

Answer by Zergling103 · Dec 21, 2011 at 06:27 AM

 var vartype : object;
 
 switch(choice) { 
     case 0: vartype = new float; break;
     case 1: vartype = new Vector3(); break;
     case 2: vartype = new string; break; 
 }

new creates an object of a given type and returns a reference to it - which in this case would be assigned to vartype. Since declaring variables inside switch statements is what is giving you trouble, this avoids it. However, I'd perform all the operations on that variable inside the switch since telling the types apart is already handled there for you.

Comment
Add comment · Show 1 · 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 flamy · Dec 21, 2011 at 06:59 AM 0
Share

ya also you can use UnityEngine.Object as its type (or just Object no difference) and it doesnt the exact same thing, wat System.Object...

because UnityEngine.Object is just an alias of System.Object.

avatar image
0

Answer by Zergling103 · Dec 21, 2011 at 06:06 AM

It's best to just find another name to use. But if you don't want to do this, you can always try to separate the variables into blocks. Variables declared inside blocks will not affect future declarations outside of that block.

 ...

 // Block one
 {
     var MyVariable : Mesh;
     ...
 }

 // Block two
 {
     var MyVariable : GameObject;
     ...
 }
 
 ...
Comment
Add comment · Show 4 · 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 spacepilot · Dec 21, 2011 at 06:15 AM 0
Share

It looks exactly as what I wrote. What am I missing here? The example I posted is using blocks.

avatar image syclamoth · Dec 21, 2011 at 06:20 AM 1
Share

You can't use '$$anonymous$$yVariable' outside of the brackets in which it is declared. I still want to know why you want to do this. It is never a good idea, and there is almost certainly a better way to achieve what you actually want to do here (since it's a means to an end, rather than an end in and of itself)

avatar image Zergling103 · Dec 21, 2011 at 06:20 AM 0
Share

Also, declared this way, you cannot use the variable outside of individual blocks. If you need to do that then you'd have to try the method I mentioned above using objects and "new".

But honestly I'd revise my script to avoid this issue altogether.

avatar image spacepilot · Dec 21, 2011 at 06:22 AM 0
Share

What is "new" doing?

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

9 People are following this question.

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

Related Questions

C#: Declaring a script as a variable 1 Answer

Checking if variable is nullable type? 3 Answers

How can you store a reference to something that you don't know the type of yet? 2 Answers

Vector3 is a 'type' but it is used like an 'variable' (C#) 1 Answer

Variable types usage 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