Problem is not reproducible or outdated
Error with calling events from other scripts
So basically on this chunk of code im trying to pass the horizontal (hsp) and vertical (vsp) speed to the physics Script so that after i call the physics Gravity those 2 variables will be changed according to gravity and soon after they will be retrieved... the problem is that even doe both variables on both scripts are public im still getting an error:
" NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Scripts/Player.cs:65) "
Note that the first script (Player) which is executing this code is attached to the object player while physics isnt so that i could manipulate the order on which its events happen on the player script
physics.vsp = vsp;
physics.hsp = hsp;
physics.Gravity();
vsp = physics.vsp;
hsp = physics.hsp;
Answer by AnOrdinarySandwich · Jan 30, 2019 at 02:35 AM
It sounds like the physics variable hasn't been set to anything in the code, but it's trying to use it anyway. Where does it get defined? Often during testing, I'll put a kill switch null test in Start for certain variables, like:
if(this.physics==null) { QuitMethod("physics is null"); }
both variables on both scripts are alredy defined as float = 0
Answer by hameed-ullah-jan · Jan 30, 2019 at 05:23 AM
you are accessing variable in physics.cs , directly through physics class. first define an object of the physcics class, then assign the gameobject (to which the physics script is attached) to this object of physics class, than access vsp and hsp through object reference. public Physics myobj; // assign from the inspecter the object to which physics script is attached // then access the variables like this myobj.vsp = vsp; myobj.hsp = hsp; myobj.Gravity(); //
I mean everything from defining the physics as a variable was alredy made but now that i've remembered that the physics script wasnt attached
Answer by Mouroz · Feb 01, 2019 at 10:14 PM
Problem solved! I thought i didnt have to attach the script Physics but now that i did its all working well!