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 castor · Jun 21, 2013 at 07:02 AM · arrayvariablefunctionaddtemporary

Add a temporary variable to an array

So I'm trying to add a temporary variable inside a function to an array. This variable is of a custom made class.

Unfortunately I can't seem to get it to work. The array increases size but when I "call" for the variable it always comes out emtpy unless I declare it outside the function..

Here is the current code where I add the variable:

 var pickUpAction : PlayerActions;
 function PickUp (selectedObject : Transform){
     GoTo(selectedObject);    
 
     //Add a pickUpAction
 //    var pickUpAction : PlayerAction; //this is where I wanted to have the variable so it disappears after being added.
         pickUpAction.interaction = PickUpAction;
         pickUpAction.selectedObject = selectedObject;
     globalActions.Add(pickUpAction);
 }

And here is the example of a function that later on uses it:

 function DoAction (){
     currentAction = (globalActions[0] as PlayerAction);
     
     currentAction.interaction ();
     globalActions.RemoveAt(0);
 }

and this is the class of the variable:

 class PlayerAction {
     var interaction : function():void;
     var selectedObject : Transform;
     var selectedDestination : Transform;
     var selectedPosition : Vector3;
 };

I'm quite new to programming so sorry if I'm missing the obvious! Any help very appreciated.

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

1 Reply

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

Answer by whydoidoit · Jun 21, 2013 at 07:35 AM

I think you are looking for:

        var pickUpAction = new PlayerAction();
Comment
Add comment · Show 3 · 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 castor · Jun 21, 2013 at 03:11 PM 0
Share

It works!!! Thank you :) What I don't understand is why it works...What is the difference between:

 var pickUpAction : PlayerActions;
 and
 var pickUpAction = new PlayerAction();

I didn't know about the "new", what difference does it make?

avatar image whydoidoit · Jun 21, 2013 at 04:07 PM 0
Share

Well the first defines an empty variable and the second actually creates one. I guess you are not using #pragma strict or it would probably not compile.

avatar image castor · Jun 21, 2013 at 04:10 PM 0
Share

Thanks for the explanation. I have the #pragma strict at the top and it still compiles. I was only getting the error when executing the function.

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

15 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

Related Questions

passing a javascript array into a function sorts it? 1 Answer

Array Problem - Error Code BCE0022 1 Answer

variable with int value not passing to command 1 Answer

How to make an array of Interactive Clothes in one variable 0 Answers

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 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