- Home /
Problem with looping concatenation with custom class
I have an array of objects, a custom class that has an attribute called 'commentUID.' I'm trying to output a string that combines the commentUID of each object with a comma in between. However, my script won't add anything past the first object, and I have no idea why. It's running through the loop...it's just not adding the commentUID strings. If I try it with another type of object, like have it add a simple string, it works as expected. Any ideas?
 var allComments : commentObj[] = new commentObj[0];
 
 class commentObj { 
 
     //Use this number to assign the Alr
     private var projectID : int = 1;
 
     var commentUID : String;
 
     var user : userInfo;
     var userPos : userPosition;
     
     var target : GameObject;
     var targetPosition : Vector3;
     
     var view : Texture2D;
     var date : System.DateTime = System.DateTime.Now;
     var text : String;
 }
 
     var CommentString : String;
     
     for (var x = 0; x<allComments.Length; x++){
         if(x == 0){
             CommentString = allComments[x].commentUID;
             Debug.Log("Ran Once");
         }
         else{
             CommentString = CommentString + "," + allComments[x].commentUID;
             Debug.Log("Ran " + (x+1) + " times");
         }
     }
I'll note that I've tried this using CommentString += allComments[x].commentUID as well.
Also, as is probably obvious, this isn't the full script - I've only included the relevant parts, not the parts that create commentObjs or add them to the allComments array. Those parts work fine.
Yeah, I remembered that I'd swapped that out and commented about it above. I've tried this with two formats:
x += y; x = x + y;
Neither one works...each time I end up with my original x, without the y concatenated to it.
edit: this was a response to a now-deleted comment
Answer by Itinerant · Mar 13, 2013 at 07:43 PM
Ok, so it looks like it was a problem with the input....I'm not really sure what caused it, though. It was typed to a String, so I wouldn't expect problems. In any case, fortunately my commentUID (while I treat it like a String) is actually purely numerical. I was able to fix my problem by changing the allComments.x.commentUID into parseInt(allComments[x].commentUID).ToString()
Why this works, I can't really say. Something is obviously wrong with my input, but darned if I know what :P
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                