How do I use current List Object [i] as the string name?

Hi Guys,

I would like to do use the current selected List object[value] as my string name.

How do I convert it? I tried to get ToString but it’s returning error message. (This line where has “// ERROR!!! THIS PART”)

Error message: BCE0023: No appropriate version of 'UnityEngine.Object.ToString' for the argument list '(String)' was found.

Thanks!

Below is my code:

#pragma strict
import System.Collections.Generic;

class TimerClass 
{
	var TimerName : String;
    var TimerTotal : float = 30 ;
    var TimerActive : boolean = false;
    var TimerRemain : float;
    
    function CountDown ()
    {
    	TimerActive = true;
    	TimerRemain -= 1 * Time.deltaTime ;
    }
 }
 
var PlayerTimer : TimerClass =  new TimerClass ();
var PlayerTimerList : List.<TimerClass>  = new List.<TimerClass >();


function Start () {
}

function Update () {
}


function AddTimer ()
{
	PlayerTimer = new TimerClass ();
    
   PlayerTimer.TimerRemain = PlayerTimer.TimerTotal;
   PlayerTimerList.Add(PlayerTimer);
   PlayerTimer.TimerName = ToString("PlayerTimerList[]"); // ERROR!!!
   //How do I use the current PlayerTimerlist *as e timer's name?*

}

function OnGUI ()
{

  • if ( GUI.Button(Rect(50, 150, 100,30), "NEW TIMER"))*
    

{
AddTimer();
}
}

ToString() is an instance method. Meaning it is used with an instance of the object class. Simply said you use it as:

string str = obj.ToString();

In your case that would go:

string name= PlayerTimerList[index].ToString();

Try like this:

var tempNum  = PlayerTimerList.Count;
PlayerTimer.TimerName = tempNum.ToString(); 

that’s all you meant I believe

EDIT:

lucky for you, I got a bit carried away:

#pragma strict
import System.Collections.Generic;

class TimerClass 
{
    var TimerName : String;
    var TimerTotal : float = 5.0 ;
    var TimerActive : boolean = false;
    var TimerRemain : float;
    var TimerBox : Rect;
    var TimerIndex : int; 
 }

var buttonRect : Rect = new Rect(50, 150, 100,30);
var PlayerTimer : TimerClass;
var PlayerTimerList : List.<TimerClass>  = new List.<TimerClass >();
var snapToGrid : boolean;
var gridWidth : int = 50;
var gridHeight : int = 50;

function Start () {
buttonRect= new Rect(50, 150, 100,30);
PlayerTimer = null;
}

function Update () {

      var inverseMousePosition = Input.mousePosition;
      inverseMousePosition.y = Screen.height-inverseMousePosition.y; 
      for(timer in PlayerTimerList){
      if(Input.GetMouseButtonDown(0) && timer.TimerBox.Contains(inverseMousePosition))
      {
         PlayerTimer = timer;
          }     
       }
      if(Input.GetMouseButtonDown(0) && buttonRect.Contains(inverseMousePosition))
      {

       if(PlayerTimer && PlayerTimer.TimerBox.Contains(inverseMousePosition))
         Debug.Log("Override Button");
       else
         AddTimer();
      }

if(Input.GetMouseButtonDown(1) && PlayerTimerList){
for(var i = 0; i < PlayerTimerList.Count;i++){
if(PlayerTimerList*.TimerBox.Contains(inverseMousePosition)){*

PlayerTimerList_.TimerActive = !PlayerTimerList*.TimerActive;
}
}
}
if(Input.GetMouseButton(0) && PlayerTimer){
if(!snapToGrid){
PlayerTimer.TimerBox.x = Input.mousePosition.x;
PlayerTimer.TimerBox.y = Screen.height-Input.mousePosition.y;
}
else
{
PlayerTimer.TimerBox.x = Input.mousePosition.x - (Input.mousePosition.x % gridWidth) ;
PlayerTimer.TimerBox.y = (Screen.height-Input.mousePosition.y) - ((Screen.height-Input.mousePosition.y) % gridHeight);
}
}*
if(Input.GetMouseButtonUp(0))
PlayerTimer = null;_

for(i = 0; i < PlayerTimerList.Count;i++){
if(PlayerTimerList*.TimerActive){*
if(PlayerTimerList*.TimerRemain >= 0)*
PlayerTimerList*.TimerRemain -= Time.deltaTime;*
else{
PlayerTimerList.RemoveAt(i); // This remove the timer
ReName();
}
}
}
}
function AddTimer ()
{
PlayerTimer = new TimerClass ();

PlayerTimer.TimerRemain = PlayerTimer.TimerTotal;
PlayerTimerList.Add(PlayerTimer);
var tempNum = PlayerTimerList.Count;
PlayerTimer.TimerName = tempNum.ToString();
PlayerTimer.TimerBox = new Rect(Input.mousePosition.x,Screen.height-Input.mousePosition.y,30,30);
}

function OnGUI ()
{
GUI.Box(Rect(buttonRect), “NEW TIMER”);

for (timer in PlayerTimerList){
GUI.Box(timer.TimerBox,timer.TimerName);
if (timer.TimerActive){
GUI.Label(timer.TimerBox,“!” + timer.TimerRemain);
}
}
}

function ReName(){
for(var i = 0; i < PlayerTimerList.Count;i++) // loop through each element of PlayerTimerList
{
PlayerTimerList*.TimerName = (i+1).ToString(); // Set name equal to current position in list (name is i+1)*
PlayerTimerList*.TimerIndex = i; //index is i*
}
}
This should give you a decent head start… it allows you to drag and place your timers! For now you can right click on one to start it… Enjoy :slight_smile:
NOTE: Updated… fixed a few bugs, got rid of the needless coroutine…

You need an index to access an object of that list (PlayerTimerList[2], for instance). Of course, you must fil it with instances of TimerClass first.

About ToString, I’m not sure how it works in uscript, but if the syntaxe is correct it will always returns ‘TimerClass’. Override the ToString function, or affect use TimerClass.name