c# - structured variables pass to method becomes zero? -


unity c# global struct variable treated local surprised need seek advise. in start:

struct st{ public float f; public bool b_toprocess;}st st; st = new st(){b_toprocess = true}; 

in update:

if(st.b_toprocess) process(st); 

somewhere in same .cs:

process(st st){     debug.log("f:" + st.f); // 0     st.f += 0.1f; if( st.f > 5){b_toprocess = false;}     debug.log("f:" + st.f); // 0.1f } 

but process run never-ending !!!! logs showed f 0 on start of each , subsequent iteration , after += 0.1f never greater 5. right should accumulative on each iteration. question is: how come st.f @ 0 on each iteration. local variable treated way passed in struct variable.

please some1 advise. thanks.

my psychic debugging powers telling me expect struct behave reference type.

in c#, structs value types.


when call process(), whole struct copied, not reference. change in struct not reflected in struct in calling method.


if make class instead, will behave reference type.

you can use ref keyword

process(ref st st) {    ... } 

and call this

if(st.b_toprocess)      process(ref st); 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -