How do I access an updated variable between two scripts in Unity with C#? -
hopefully isn't detail, i'm not used asking programming questions.
i'm attempting 3d video game development unity 3d course that's on udemy, though using c# instead of javascript. finished tutorial involves creating space shooter game.
in it, shield created user when pressing button. shield has "number of uses" variable not used time tutorial has finished. i'm trying add in, , have managed implement each use, decrease number of uses remaining, , no longer able instantiate shield once number <=0.
this variable stored on player, , if print player, returns value expect.
however, i'm using separate scenemanager.cs (this tutorial placed lives, , score, , timer variables ) print numbers gui. problem cannot number of uses variable stay current when try print scene manager... registers initial value, doesn't update after that.
here player script
using unityengine; using system.collections; public class player_script : monobehaviour { // inspector variables public int numberofshields = 2; // number of times user can create shield public transform shieldmesh; // path shield public keycode shieldkeyinput; // key activate shield public static bool shieldoff = true; // initialize shield "off" state public int numberofshields { get{return numberofshields;} set{numberofshields = value;} } // update called once per frame void update() { // create shield when shieldkey has been pressed player if (input.getkeydown (shieldkeyinput)) { if(shieldoff && numberofshields>0) { // creates instance of shield transform clone; clone = instantiate (shieldmesh, transform.position, transform.rotation) transform; // transforms instance of shield clone.transform.parent = gameobject.transform; // set shield on position shieldoff = false; // reduce numberofshields left numberofshields -=1; } } print ("numberofshields = " + numberofshields); } public void turnshieldoff() { shieldoff = true; } }
when run "print ("numberofshields = " + numberofshields);" value expect. (astroids trigger turnshieldoff() when collide shield.
over in scene manager however... code i'm running:
using unityengine; using system.collections; public class scenemanager_script : monobehaviour { // inspector variables public gameobject playercharacter; private player_script player_script; private int shields = 0; // use initialization void start () { player_script = playercharacter.getcomponent<player_script>(); } // update called once per frame void update () { shields = player_script.numberofshields; print(shields); } // gui void ongui() { gui.label (new rect (10, 40, 100, 20), "shields: " + shields); } }
any idea i'm doing wrong prevents shields in scenemanager script updating when numberofshields in player_script updates?
i think might have assigned prefab playercharacter gameobject variable instead of actual in game unit. in case print default shield value of prefab. instead of assigning variable via inspector try find player gameobject in start function. can example give player object tag , then:
void start() { playercharacter = gameobject.findgameobjectwithtag("player"); }
Comments
Post a Comment