- Home /
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613083329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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