- Home /
The question is answered, right answer was accepted
WARNING : Assignment to temporary.
What does this mean and how do I get rid of it?It keeps appearing when I try to loop through a list and modify a boolean var.
 #pragma strict
 import System.Collections.Generic; 
 
 var theOpenList     : List.<Cel> =new  List.<Cel>();
 var i : int;
 
 
 function Update () 
 {
     for(i = 0 ; i < theOpenList.Count ; i++)
     {
         
         if(theOpenList.Count > 0)
         {
             theOpenList[i].startSearch = true; 
             theOpenList[i].inList = true;                      
                     Instantiate(cube,theOpenList[i].celPos,transform.rotation);
                     theOpenList[i].cubeCheck = false;
             }
      }
}
My list is filled with elements from a class named Cel......and if I loop through the list changing the boolean var it gives me this warning.
This is my Cel class
 #pragma strict
 
 class Cel extends System.ValueType
 {
     
     var h : float;
     
     var obj       : Transform;
     var enemy       : Transform;    
     var distance  : Vector3;    
     var celPos       : Vector3;    
     var color       : Color;        
     
     var cubeCheck : boolean;
     var inList : boolean;        
     var startSearch : boolean;
     
     
     
     
     function Cel (position : Vector3, object : Transform,  enemyPos : Transform, parent : Cel, hCost : float)
     {
         h = hCost;
         celPos                = position;
         obj                 = object;    
         enemy                 = enemyPos;
         
     
         if(enemy != null)
         {
             distance = obj.position - enemy.position;
             h         = distance.magnitude * 10;
         }    
     }
     
     function Start()
     {
         startSearch = false
         cubeCheck = true;
         inList = false;
     
     }
 }
 
 I dont know waht the error means....but I need help and an explanation. 
Duplicate of http://answers.unity3d.com/questions/133457/bcw0006-warning-assignment-to-temporary.html.
Please close.
Answer by Peter G · Aug 05, 2013 at 03:20 PM
I think that post does a good job explaining it, but I think a slightly different solution would save you a little work. Rather than call the constructor to create a new one from scratch, clone the original, change the clone, then copy it back into the array.
 if(theOpenList.Count > 0) {
      var temp = theOpenList[i];
      //copy all the values over to a temporary object.
      temp.startSearch = true; 
      temp.inList = true;
      temp.cubeCheck = false;
      //edit all the necessary values.
 
      //Reassign the copied object to the original.
      theOpenList[i] = temp;                      
      Instantiate(cube,theOpenList[i].celPos,transform.rotation); 
 }
Follow this Question
Related Questions
C# custom class array error in adding a new entry. 1 Answer
Error message when accessing class array 1 Answer
adding classes to arrays 2 Answers
2d array.What is the difference between these two? 1 Answer
Reset and arrays. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                