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 Youngapprentice · Nov 30, 2011 at 03:20 AM · variableslocaleasyquick

Local Variables

Hello, all. This should be simple and quick. I have a prefab. This prefab has a script that is a timer. The script has a variable that the time of the timer is stored in. The problem is, I will have more than one instance of this prefab, and eery time I instantiate a new one, it picks up where the timer from the first one left off. Is there any way I can make it so that the other duplicates do NOT use the same variables, but keep separate copies of the same variable for themselves? thanks - YA

Here is the code.

 var yellowTex : GUITexture;
 private var xpos : float;
 private var ypos : float;
 private var scale : float = -0.8;
 private var scaledelta : float;
 private var position = Vector3(-0.002426388,0.205147,1);
 private var xdelta : float;
 private var ydelta : float;
 private var created : boolean;
 private var yellow : GUITexture;
 var TravelTime : float = 2;
 var TimeStart : float = 0.0;
 //Beginning coordinates are (-0.002426388,0.205147,1);
 //Ending coordinates are (-0.02,0.02,1);
 function Update () {
     if(Input.GetButtonDown("Vertical")){
     yellow = Instantiate(yellowTex, position, Quaternion.identity);
     created = true;
     yellow.transform.localScale = Vector3(-0.8, -0.8, 1);
     }
     if(yellow){
     //calculating x and y positions
     xdelta = ((-0.02 - -0.002426388)/TravelTime)*TimeStart;
     ydelta = ((0.02 - 0.205147)/TravelTime)*TimeStart;
     ypos = 0.205147 + ydelta;
     xpos = -0.002426388 + xdelta;
     //updating position
     yellow.transform.position = Vector3(xpos,ypos,1);
     //calculating scale values
     scaledelta = ((0 - -0.8)/TravelTime)*TimeStart;
     scale = -0.8 + scaledelta;
     //updating scale
     yellow.transform.localScale = Vector3(scale,scale,1);
     }
     if(created == true){
     CountUpwards();
     }
 }
 //The timer function
 function CountUpwards(){
     TimeStart += Time.deltaTime;
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Nov 30, 2011 at 04:21 AM

Posting code would help. My guess is your variables are 'static'? Should not be.

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 Eric5h5 · Nov 30, 2011 at 04:24 AM

Variables are separate instances by default. I assume you're using static variables; you should never use them unless you're sure you only want one instance.

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 Youngapprentice · Nov 30, 2011 at 05:43 PM 0
Share

Here is the code. It is attached the prefab. Every time I instantiate a new one, It picks up where the other one left off, not where the code tells it to. The math you see is simple tweening of GUITextures.

I'll post the code as an answer

avatar image Eric5h5 · Nov 30, 2011 at 06:10 PM 0
Share

Please do not post comments as answers.

avatar image Youngapprentice · Nov 30, 2011 at 07:49 PM 0
Share

Sorry. i changed it and put it into the original post. So how do I fix this?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Accessing variables from seperate scripts 1 Answer

Accessing local system ( File Browser ) 2 Answers

Editing local variables in another script 2 Answers

Handling a large number of behind the scenes variables 1 Answer

Another collision detection issue 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