- Home /
Do asleep rigidbodies prevent from modifying its variables in runtime?
Hello,
I have a player gameObject with some scripts attatched to it. The player gameObject is with 0 movement most of the time, and I have different scripts modifying variables within each other. But the problem is that they are not modifying each other, and I think that may be because the gameObject that contains the scripts is asleep. And even if I'm right I don't manage to solve it just with the WakeUp() function.
For example, I have one script that makes the player pick up an object when hes on reach (PickUpObject.cs), and another one that makes him throw it (ThrowObject.cs). If the player doesn't throws it and moves, PickUpObject.cs returns the object where it was before picking it. In this case the objects are ingredients (because the game theme):
public class PickUpObject : MonoBehaviour {
public GameObject ingredient;
public bool hasIngredient;
and
public class ThrowObject : MonoBehaviour {
PickUpObject pickUpObject;
void Awake () {
pickUpObject = GetComponent<PickUpObject> ();
}
void Throw () {
pickUpObject.ingredient.rigidbody.useGravity = true;
pickUpObject.ingredient.rigidbody.AddForce(angle*force, ForceMode.Impulse);
rigidbody.WakeUp ();
if (rigidbody.IsSleeping ()){
print ("is sleeping");
}
pickUpObject.hasIngredient = false;
print (pickUpObject.hasIngredient);
}
}
There's more logic I didnt posted here, but basically, in order to not put down an already thrown object, when it's thrown I want to set hasIngredient to false, and the funny thing is that when I print the state of hasIngredient from the ThrowObject script it prints false, but in the inspector (where PickUpObjects.cs shows it because is public) it appears as true, and in fact the game behaves as such, so I assume that it's not being really modified. And also that there're an instance of PickUpObject where its false (the one that ThrowObject.cs picks) and the original at true? Are not this variables stored by reference? Is all that because the object is sleeping?
I also tried to wake up the gameObject, even waiting to the next frame to print if its been awake, but always prints the "is sleeping" string.
How does people deal with this kind of thing?
Answer by Owen-Reynolds · Jan 21, 2015 at 06:17 PM
No, sleeping rigidbodies don't have any effect on the script. The script doesn't care about RB.sleep status (I've never directly tested this, but I feel like I would have noticed it by now. Could test with a script that sleeps the RB, counts off 300 frames, then prints something.) All sleep does is tell the physics step to skip that object.
You're probably seeing the "is sleeping" message from different scripts or previous frames. Try having public bool slp;
with slp=rigidbody.Issleeping();
in Update, which will allow you to "see" the current sleep status of the object.
This looks like a typical debugging dead-end. Thinking about this sleep stuff is preventing you from finding the real code bug.