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 moonLite · Nov 04, 2012 at 04:18 PM · javascriptlistaddgeneric.list

How to use list to add new items & function?

Hi Guys,

I'm trying to use generic list in javascript or unityscript to: 1) add new timer in the game scene live itself 2) add new, edit & store each player's personal gold or money

My questions:
1a) how do I add new item to my list in the game scene live?

 myList.Add(theItem);   // add an item to the end of the List
 myList[i] = newItem;   // change the value in the List at position i

1b) I know above coding is how it works, but I don't know how to apply it to my coding in this case. Could I have a few examples of applying to the my own generic coding below?


2) how do I dynamically add the timer function in the game scene live again? For instance each time a button is press or a player game, a new timer is assign and calculate by itself.
Currently, I can hard code like a few timers in the script by itself. But if I wish more timers, then it will be too messy, so I would like to be able to dynamically add new timer and count the timer.


3) How to remove, and edit the value in my 2 generic list, `differentPlayerGold` & `timerList`?

Below is my Generic List coding:

 #pragma strict
 import System.Collections.Generic;
 class Timer
 {
      var timerCounter : float = 0;
     var timerMax : float = 5.5;
     var timerActive : boolean = false ;
 }
 var differentPlayerGold : List.<int> = new List.<int>(); // Store each player's gold
 var timerList = new List.<Timer>(); // store different player's time limits
 
 function Start () 
 {
   differentPlayerGold.Add(5);
   differentPlayerGold.Add(7);
 }
 
 function Update () 
 {
     Debug.Log(differentPlayerGold);
 }

I read some of the links provided & previously ask questions. But I don't get it.
Below are my reference links:


1)http://answers.unity3d.com/questions/327065/explanation-on-how-to-use-list-in-unityscript.html
2)http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
3)http://answers.unity3d.com/questions/329133/what-are-lists.html

Below is my current Timer coding:

 #pragma strict
 class TimerClass 
 {
    var FirstTimer : float = 0 ;
    var FirstTimerActive : boolean = false ;
    var SecondTimer : float = 0 ;
    var SecondTimerActive = true ;
    
    var FirstTimerTime : float;
 }
 var PlayerTimer : TimerClass = new TimerClass ( ) ;
 //or 
 //var PlayerTimer = new TimerClass ( ) ;
 //var PlayerTimer : TimerClass2 ;
 
 function Awake ()
 {
     PlayerTimer.FirstTimerTime = PlayerTimer.FirstTimer;
 }
 
 
 function Update ( )
 {
 //    Debug.Log(PlayerTimer.FirstTimerActive);
 //    Debug.Log (PlayerTimer.FirstTimer);
 
 }
 function OnGUI ()
 {
 
    if ( PlayerTimer.FirstTimerActive )
    {
      PlayerTimer.FirstTimer -= 1 * Time.deltaTime ;
       if ( GUI.Button(Rect(50, 150, 100,30), "PAUSE"))
       {
          PlayerTimer.FirstTimerActive = false ;
       }
       GUI.Box (Rect (160, 150, 100,30), "UNPAUSE");
    }
    
    if ( !PlayerTimer.FirstTimerActive && PlayerTimer.FirstTimerTime != PlayerTimer.FirstTimer)
       {
       if( GUI.Button(Rect(160, 150, 100,30), "Resume"))
       {
          PlayerTimer.FirstTimerActive = true ;
       }
       GUI.Box (Rect (50, 150, 100,30), "PAUSE");
 
    }
    
    
    
 // 2ND TIMER
 //   if ( PlayerTimer.SecondTimerActive ){
 //      PlayerTimer.SecondTimer -= 1 * Time.deltaTime ;
 //      if ( GUI.Button(Rect(150, 150, 100,30), "PAUSE2")){
 //         PlayerTimer.SecondTimerActive = false ;
 //      }
 //   }else{
 //      if( GUI.Button(Rect(200, 150, 100,30), "Resume2") )
 //      {
 //         PlayerTimer.SecondTimerActive = true ;
 //      }
 //   }
 
         // stop button  timer 1
     if (!PlayerTimer.FirstTimerActive && PlayerTimer.FirstTimer == PlayerTimer.FirstTimerTime )
     { 
         if (GUI.Button(Rect (50, 100, 210,30), "START"))
         {
             PlayerTimer.FirstTimerActive = true ;
         }
         GUI.Box (Rect (50, 150, 100,30), "PAUSE");
         GUI.Box (Rect (160, 150, 100,30), "UNPAUSE");
     }
     
     //reset button timer 1
       if (PlayerTimer.FirstTimer != PlayerTimer.FirstTimerTime )
     { 
         if (GUI.Button(Rect (50, 100, 210,30), "RESET"))
         {
             PlayerTimer.FirstTimerActive = false ;
             PlayerTimer.FirstTimer = PlayerTimer.FirstTimerTime;
         }
     }
     
 }

I have yet combine, I didn't combine so that it won't be so messy and easier to understand.
Please let me know if I need to provide anything more, thanks!

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

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

A node in a childnode? 1 Answer

Maximum elements in a Generic List 0 Answers

Java Script command list 3 Answers

FindIndex in a List 1 Answer

How to make a List Constructor? 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