- Home /
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[i] as e timer's name?
}
function OnGUI ()
{
if ( GUI.Button(Rect(50, 150, 100,30), "NEW TIMER"))
{
AddTimer();
}
}
Answer by Seth-Bergman · Nov 16, 2012 at 11:22 AM
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[i].TimerBox.Contains(inverseMousePosition)){
PlayerTimerList[i].TimerActive = !PlayerTimerList[i].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[i].TimerActive){
if(PlayerTimerList[i].TimerRemain >= 0)
PlayerTimerList[i].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[i].TimerName = (i+1).ToString(); // Set name equal to current position in list (name is i+1)
PlayerTimerList[i].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 :)
NOTE: Updated... fixed a few bugs, got rid of the needless coroutine..
That is the fact that I cannot really figure out what he is trying to convert...How many, a particular object of the list...I dunno...
good point, on second read, I think it may also be what you said
Hi Seth,
It works but if I delete one of the timer, then a few timers will have the same time because of the count.
Is there other methods?
first off, when we want to get rid of a timer, we use the line:
PlayerTimerList.RemoveAt(timer); // This remove the element of the list at index "timer"
when we remove an element from the list, the index for the other ones (with a higher index) automatically shift over by one.
immediately after this, we call ReName(), which simply loops through and sets the name equal to the index + 1, in other words the position.. for example, if the index is 0, the timer is in the 1st position, so we set the name to (index + 1)..
So, as long as we always remember to call ReName(), each TimerName always matches its position already.. but if you like just make ReName like this:
function ReName(){
for(var i = 0; i < PlayerTimerList.Count;i++) // loop through each element of PlayerTimerList
{
PlayerTimerList[i].TimerName = (i+1).ToString(); // Set name equal to current position in list //name is i+1
PlayerTimerList[i].TimerIndex = i; //index is i
}
}
I edited my code to be a bit cleaner.. you can drag timers, or right click them to set off (but only 1 at a time, or you'll get errors).. also can turn on snap..
Good Luck from here!
It's no bother, I'm happy to help someone who's obviously interested in learning.. Ok so:
1) yes, exactly correct.. ReName sets the name DIRECTLY from the index of the member
2)this one explains nicely
http://www.dotnetperls.com/removeat
http://www.dotnetperls.com/list
You can easily see for yourself, because the List PlayerTimerList is public in our script.. Just make sure "maximize on play" is not checked, so you can hit play and still see the inspector.. then select the object that contains the script.. as you add/change timers you will see the List change.
Glad to help, happy coding!
(btw, updated my answer script a bit more...)
Answer by fafase · Nov 16, 2012 at 11:18 AM
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();
Hi @fafase ,
Thanks for the suggestion, but it's returning to me 2 errors. How do I access to the index?
$$anonymous$$ identifier: 'index'.
The best overload for the method 'System.Collections.Generic.List.<TimerClass>.get_Item(int)' is not compatible with the argument list '(error)'.
What are you trying to convert? index is the index of the list. You know that your list is a collection of whatever you store in. They are stored as 0,1,2,3,4, and so on. You access one of them with an index or a value or anything that is an integer.
Let's sum it up, you want to create a new object, add it to the list and then gets its name:
function AddTimer (){
PlayerTimer = new TimerClass ();
PlayerTimer.TimerRemain = PlayerTimer.TimerTotal;
PlayerTimerList.Add(PlayerTimer);
var count = PlayerTimer.Count-1;
PlayerTimer.TimerName =count.ToString();
}
Note that Count returns how many items there are in the list but the indexing starts from 0 so I subtract 1. This is not going to store "two" but the string "2". If you were to get a string of a number, you might have to do it by hand with a hell of a lot of if-else if.
One side note, it is easier to read code when conventions are respected. Class name are upper case but instance objects are lower case. It is not compulsory but just a common use.
OOOpsy, it is not the object you are counting but the list....
var count = PlayerTimerList.Count-1;
" the 1st name of the timer is 0"Yes a list/array or whatever collection in C# and many languages starts at 0. If you want it to start from 1 remove the -1.
You can use any value you want for the index as long as it is an integer:
var num:int = 10;
list[num] = 20;
I just place 20 in the 11th index (starts from 0).
It also works for data that represent an integer:
enum Day{$$anonymous$$onday,Tuesday, Wednesday, Thursday, Friday};
Hours[$$anonymous$$onday] = 8;
Hours[Tuesday] = 7;
This is because $$anonymous$$onday is equal to 0 and Tuesday to 1 and so on...See about enum.
What about this?:
var num:int = 10;
list[num].value = 20;
list[list[num].value].value = 30;
I put 20 in the 11th index and then placed 30 in the 21th index.
var currentIndex:int = -1;
function AddObject(){
//Creating object
list.Add(newObject);
currentIndex++;
}
function GetLastObject(){
return list[currentIndex].variableName;
}
This way, each time you create a new object, you increase the index. You use that index to retrieve the last one. Now if you need to reach one in particular you would either need to know where it is or use a function that iterates through the list and returns that object but that is another issue. I suggest you look for List on the msdn documentation.
Answer by FakeBerenger · Nov 16, 2012 at 11:18 AM
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
Your answer
Follow this Question
Related Questions
Cannot convert 'String' to 'float (but im actualy converting a float to a string) 2 Answers
A node in a childnode? 1 Answer
Start timer with Input 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
timer does not update 0 Answers