So I made a script from a youtube guide, that is supposed to pick up a log by pressing the mouse button, and let it go again when I stop holding the mouse button in. I pick up the log fine, but it won’t let go of it again.
var target : Transform;
function Update () {
}
function OnMouseDown () {
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
this.transform.parent = GameObject.Find("FPSController").transform;
this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
}
function OnMouseUp () {
this.transform.parent = GameObject.find("FPSController").transform;
this.transform.parent = null;
}
Generally you should use SetParent instead of assigning the parent the way you are.
Next, using GameObject.Find is slow, so there is probably a better way to do it.
Finally…the script seems a bit weird to me. Why are you changing parents twice in a row? And why are you setting the transform…equal to the transform?
Now, I’m guessing this script is on a “log” so that when you pick it up, it ties it to the FirstPersonCharacter gameobject so that when you move it gives the impression it’s carried. When you let go of the mouse, it should remove the parenting, thus dropping the log. At least that is what it should do.
So to test, make sure OnMouseUp is being called (debug in there). Make sure the log is losing it’s parent. Since you are following a tutorial, make sure you didn’t miss setting something up.
Everything @Brathnann said, except much more emphatically recommending not using GameObject.Find - there is literally never a time when GameObject.Find is the right solution for in-game code. Here is a writeup on why GameObject.Find stinks and some ways how you might go about replacing it - probably a singleton in this case.
While there are issues with the way that code is written, I strongly suspect that none of the above advice fixes your immediate problem. I can almost guarantee that OnMouseUp is simply never being called. If the mouse cursor is not still over the log when you release it, it won’t be called. You can confirm this by putting Debug.Log(“something”); in the OnMouseUp function - if you don’t see anything in Unity’s console, that function is not being called. You can use OnMouseExit to detect when the cursor has left the object, or you can use Update and check for this:
Appears OnMouseUp is actually still called (I honestly had to double check this earlier myself)
Per Unity’s doc
Note that OnMouseUp is called even if the mouse is not over the same GUIElement or Collider as the has mouse has been pressed down on. For button-like behavior see: OnMouseUpAsButton.
I read that too while answering this question, and my interpretation of that sentence was exactly the opposite of yours: I inferred that it meant OnMouseUp would be called on the object the mouse was now hovering over whether or not the mouse was over it when you started the click. Given the code Though after reading your reply, it does seem clear how that sentence can be interpreted in two opposite ways.
I just tested in the editor and the behavior does appear to match your interpretation of that sentence - I think I’m going to bugreport that page so that they can rewrite that section to be clearer.
Though this does make the original question baffle me, as setting transform.parent to null in OnMouseUp has no earthly reason to not work, no matter how odd the logic is that precedes it.
It would not work if that first line threw a nullRef, but then OnMouseDown wouldn’t work.
@Ekmul , the code makes absolutely no sense! If you copied that from a youtube guide, find a different guide because the one you’re looking at has no clue what they’re doing.
Ah, I can see how that could be interpreted your way as well.
And as @Baste confirms. The code really doesn’t make sense. So either it’s a bad guide or there are extra things in there that you maybe skipped in the process of just getting the code you thought you needed.