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 16, 2012 at 11:12 AM · javascriptlisttimernametostring

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();
       }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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..

Comment
Add comment · Show 14 · 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 fafase · Nov 16, 2012 at 11:25 AM 0
Share

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...

avatar image Seth-Bergman · Nov 16, 2012 at 11:29 AM 0
Share

good point, on second read, I think it may also be what you said

avatar image moonLite · Nov 16, 2012 at 11:29 AM 0
Share

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?

avatar image Seth-Bergman · Nov 16, 2012 at 11:09 PM 1
Share

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!

avatar image Seth-Bergman · Nov 17, 2012 at 07:49 AM 1
Share

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...)

Show more comments
avatar image
1

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();
Comment
Add comment · Show 10 · 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 moonLite · Nov 16, 2012 at 11:34 AM 0
Share

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)'.
avatar image fafase · Nov 16, 2012 at 11:47 AM 0
Share

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.

avatar image fafase · Nov 16, 2012 at 12:06 PM 1
Share

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.

avatar image fafase · Nov 16, 2012 at 12:28 PM 1
Share

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.

avatar image fafase · Nov 16, 2012 at 01:59 PM 1
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Show 1 · 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 moonLite · Nov 16, 2012 at 11:35 AM 0
Share

Hi @FakeBerenger,

Could you give examples of it?

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

12 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 avatar image avatar image

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


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