Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

unity use of possibly unassigned field struct

//If you're trying to access a struct that's not initialized,
//you may get this error
//I.E.
myStruct newStruct;
if(someCondition){
newStruct = otherInstanceOfMyStruct;
}
Debug.Log(newStruct.data);
//This will give the error because there's no way to tell if the if statement
//will actually go through and set the struct
//so...

myStruct newStruct = new myStruct();
if(someCondition){
newStruct = otherInstanceOfMyStruct;
}
Debug.Log(newStruct.data);

//Setting "myStruct newStruct = new myStruct();" will ensure
//that you at least have the default values of the struct
 
PREVIOUS NEXT
Tagged: #unity #possibly #unassigned #field #struct
ADD COMMENT
Topic
Name
5+9 =