Please can someone with more brains tell me what is wrong with my code?

I’ve got a cabinet with 6 drawers which each drawer having this script attached to it. Can someone tell me if they see any problems with it cause it just isn’t working. I have my distance to be opened on the z axis, but it doesn’t move at all. The function is being called from the interface and is actually being called. I’ve also debugged each if statement and that works aswell.

public class Drawer : MonoBehaviour, IInteractable {
    public float distanceToBeOpened;

    private Vector3 closedPos;
    private Vector3 openPos;
    private bool isClosed;
    // Use this for initialization

    public void Start(){
        isClosed = true;
        closedPos = this.transform.position;
        openPos = new Vector3 (this.transform.position.x, this.transform.position.y, this.transform.position.z + distanceToBeOpened);
    }

    public void Interact(){
        Debug.Log ("Trying to Interact");
        if (isClosed) {
            transform.position = openPos;
            isClosed = false;
        }
        else{
            transform.position += closedPos;
        isClosed = true;
        }
    }
}

What value do you have assigned to distanceToBeOpened?

If Interact is being called, it should work, although the += closedPos probably isn’t what you want. that shouldn’t matter for it moving at all.

Yeah sorry I just put the += afterwards just to check I wasn’t doing something ridiculous, it was a last ditch effort, despite the fact that I believe that just transform.position = closedPos would work fine.

I have the average value set to -0.4f which should be fine. I’ve set that in the inspector for each drawer as some are slightly open. Not one opens, it just seems really strange. I’ve also debugged the bool isClosed aswell and it changes accordingly with each call.

This also assumes that your object is aligned to the world Z axis since you’re only adding to Z position. I would expect that you would want to be offsetting in the local Z axis, which may involve all 3 positional axes potentially.

Perhaps more like this?

public class Drawer : MonoBehaviour, IInteractable {
    public float distanceToBeOpened;

    private Vector3 closedPos;
    private Vector3 openPos;
    private bool isClosed;

    private void Start() {
        isClosed = true;
        closedPos = transform.position;
        Vector3 offset = transform.forward * distanceToBeOpened;
        openPos = transform.position + offset;
    }

    public void Interact() {
        Debug.Log("Trying to Interact");
        transform.position = isClosed ? openPos : closedPos;
        isClosed = !isClosed;
    }
}

Forgive me for adding my own style to your logic, if you don’t prefer that by all means change it back.

Thanks for the reply Jeffrey.

I’ve just tried your script and the same problem, nothing is happening. The object just isn’t moving whatsoever. I’m completely puzzled by this.

Yes I only want to move the object along the Z axis. I really don’t understand at at all.

So if you manually change it in the inspector, .4f moves it enough? (it’s just a really small value is why I ask). Maybe even do something drastic and add 100 to it. Just to see what happens.

Also maybe print out transform.position in each if statement to see if it changes. Just some things to try.

Also note that the inspector may be showing local position and not world. It should still move by your code.

1 Like

Interesting. Do me a favor and run with these debug statements. Make sure you’re not calling Interact twice for some reason. Make sure you have your console not collapsing values otherwise you’d miss a duplicate debug statement perhaps.

This is some classic carpet-bomb debugging:

private void Start() {
    isClosed = true;
    closedPos = transform.position;
    Debug.Log("Closed Position: " + closedPos);
    Vector3 offset = transform.forward * distanceToBeOpened;
    openPos = transform.position + offset;
    Debug.Log("Open Position: " + openPos);
}

public void Interact() {
    Debug.Log("Current Position: " + transform.position);
    transform.position = isClosed ? openPos : closedPos;
    isClosed = !isClosed;
    Debug.Log("New Position: " + transform.position);
}

If those values output in the console make sense and only output once per interaction, then something outside of this script is affecting the movement.

Hmmm… strange. The positional coordinates are actually changing. They read (-1, 0.6, 12.7) for old position and (-0.6, 0.6, 12.7) for the new position so it’s definitely moving them, it just doesn’t move them on my game or scene window?

Wow, It’s just moving the collider and not the gameobject, By chance I’ve just seen it in the scene window while debugging in the game window. Is this normal? I thought transform.position would relate to the gameobject and not the collider?

If your collider is on it’s own gameobject and the script is on the collider gameobject, it will move it.

If that is not the case, like I said, try a bigger value. .4 is really small to move something.

My guess is the script is on a child object and the child is moving.

EDIT - just posted that it moved the collider. So yeah, something in your hierarchy is messing it up.

1 Like

My script is on a child object, this would be the cause oif the problem?

Yes. transform.position returns the position of the gameobject the script is attached to.

The thing is, My mesh renderer is attached to this child object, aswell as the box collider and the script. It’s the child I want to move yet it only moves then collider by the looks of it

Can you show a screenshot of your hierarchy with the object that has the script on it selected?

http://imgur.com/a/0etso

Broken link. You should also be able to drag the image into the post (not sure if there is a limit on size with that feature or not)

You have those objects marked as Static, uncheck that since they’re movable.

Thanks jeff, I’ve unticked lightmap static, They objects still won’t move however.