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 treep78 · Feb 12, 2012 at 11:22 PM · arraysvariables

Locking the value of a variable, or something similar

So I'm writing a program and have run into a problem with variables.

 var someArray : array;
 var someVar : CustomClass;
 
 function Update()
 {
    var tempVar = someVar;
    someArray.Push(tempVar);
    //call a function which resets someVar
 }

What I want is for 'tempVar' to be put into the array with the values that 'someVar' has during that particular moment. But after I put it into the array it keeps updating itself to whatever the current value of 'someVar' is. How do I make this do what I want it to?

Comment
Add comment · Show 3
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 AprendaGames · Feb 13, 2012 at 12:02 AM 0
Share

I tried this a second ago and it seems to work fine. Is there something more I need to know?

var someArray:Array; var someVar:int = 0;

function Start()

{

 someArray = new Array();

}

function Update()

{

var tempVar:int = someVar; someArray.Push(tempVar); //call a function which resets someVar print(someArray); someVar++;

}

avatar image treep78 · Feb 13, 2012 at 01:04 AM 0
Share

This gives more detail on what I'm trying to do.

 var action : TestClass;// 'TestClass' contains a single variable 'number' which is a string
 var actionArray : Array = new Array();
 var num: int = 0;
 
 
 
 function resetAction() // reset all properties for action to default
 {
     action.number = "";
 }
 
 
 function Update()
 {
     num++;//increase num by one
     action.number = ""+num;//set action.number to be the string of num
     var tempAction : TestClass = action;// create a varriable'tempAction' also of class 'TestClass'
     actionArray.Push(tempAction);// 'tempAction' to the 'actionArray' array
     resetAction(); //reset the value of 'action.number'
     if (actionArray.length == 3)// check the property 'number' for the instances of 'action' in the 
     {                            // 'actionArray' when in contains three instances of 'tempAction'
         var action1 : TestClass = actionArray[0];
         var action2 : TestClass = actionArray[1];
         var action3 : TestClass = actionArray[2];
         Debug.Log(""+action1.number+action2.number+action3.number);//this currently displays the 
     }                                        //value of 'number' for all three to be empty but I want them to retain their values
 }
avatar image Tseng · Feb 13, 2012 at 01:32 AM 0
Share

Please for gods sake, learn some basic program$$anonymous$$g before attempting to make a game... and learn about references

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by treep78 · Feb 13, 2012 at 04:45 AM

Your code worked for that, but when I tried to apply the same technique to a scaled down version of the code I'm working with it didn't work and the values I was trying to save were erased when I cleared the var 'action'.

 var action : ActionClass;
 var actionArray : Array = new Array();
 var num: int = 0;
 
 
 
 function resetAction() // reset all properties for action to default
 {
     action.doer = null;
     for(var i :int = 0; i < action.target.length; i++)
         {
             var resetTarget : CharacterClass = action.target[i];
             action.target.RemoveAt(i);
             resetTarget.targeted = false;
             i -= 1;
         }
     action.target.Clear();
     action.actionType = "";
     action.attackAction = null;
     action.stage = "building";
 }
 
 
 function Update()
 {
     num++;
     action.actionType = ""+num;
     var tempAction : ActionClass = action;
     actionArray.Push(tempAction);
     resetAction();
     for(var i:int = 0; i < actionArray.length; i++)
     {
         var tempActionInfo : ActionClass = actionArray[i];
         Debug.Log(tempActionInfo.actionType);
     }
 }
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
avatar image
0

Answer by Kaze_Senshi · Feb 13, 2012 at 01:00 AM

Well I don't know the types of your variables, but I think that you can try to make a "clone" of your variable. For example, let be a Vector3, instead of just attribute the variable with the tempVar, you can make tempVar be a new Vector3 and just copy its informations .

 var someArray : array;
 var someVar : Vector3;
 
 function Update()
 {
    var tempVar : Vector3 = Vector3( someVar.x, someVar.y, someVar.z ); // Change here
    someArray.Push(tempVar);
    //call a function which resets someVar
   ...
 }
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 treep78 · Feb 13, 2012 at 01:28 AM 0
Share

for some reason this doesn't seem to work with custom classes

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

7 People are following this question.

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

Related Questions

Getting a variable from an object 1 Answer

GameObject [,] variable will it be addresses? 1 Answer

Is it possible to store variables in arrays? 1 Answer

How do variable arrays work? 2 Answers

Organizing variables in the inspector 5 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