Object wont translate

So I have a script and at the end of all the code I put a translate thing so it goes up high in the air. For some reason it isn’t working. I even put a debug.log after it and it ran, so the code was executing. does anyone know why?

using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;

public class PickUp1 : MonoBehaviour
{

    public static bool IngredientCollected1 = false;





    void Update()
    {
        RaycastHit hit;

        if (Input.GetKeyDown("e"))
        {
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))   
            {
                Debug.Log("Raycast Hit!");
                if (hit.transform.CompareTag("Selectable")) Debug.Log("Object is selectable");
                {
                    Debug.Log("If statement successful");
                    PickUpFunction(hit.transform);
                }
            }
        }
    }
       
        public void PickUpFunction(Transform targ)
    {

        var rb = targ.GetComponent<Rigidbody>();
        var rend = targ.GetComponent<MeshRenderer>();
        var col = targ.GetComponent<BoxCollider>();
        if (rb != null) rb.useGravity = false;
        IngredientCollected1 = true;
        targ.transform.Translate(0, 1000, 0);
        Debug.Log("object moved");
    }
}

Should be PickUpFunction(hit.collider.gameObject.transform) instead of PickUpFunction(hit.transform)

See Unity - Scripting API: RaycastHit

Thank you, that makes sense!

@AnthonySharp It didn’t give errors, but it didn’t work. Do you know why?

Hmmmm… Maybe try change targ.transform.Translate(0, 1000, 0); to targ.Translate(0, 1000, 0);

If that doesn’t help then maybe try PickUpFunction(hit.rigidbody.gameObject.transform) or

Okay so it turns out I was wrong and that PickUpFunction(hit.transform) should work perfectly fine … in fact it might even be the most correct way of doing it (sry I’m wayyy too sleep deprived).

These lines look suspicious, I have never seen an if statement like that before :

 if (hit.transform.CompareTag("Selectable")) Debug.Log("Object is selectable");
{

Maybe put the Debug.Log inside the body {} … but I doubt that’s the bug.

Does Debug.Log(“object moved”); get called?

Sorry I can’t be of more help, nothing seems to jump out at me as being wrong with the code. Perhaps you have another script interfering and resetting its position? That’s a common problem.

I don’t think I have any more scripts that are interfering with its location. The object moved debug.Log does in fact run, but translate doesn’t.

Could always try setting the position directly:

targ.position += new Vector3(0, 1000, 0);

Hmmm… That didn’t seem to work either. Do you know what the += means? i have never used it

One of two things is happening here:

  • Some other script is moving the object back.
  • You’re moving the wrong object with this script.

Liberal usage of Debug.Log() will help you figure out which.

1 Like

I don’t think anything else is moving it back. No other script translates this object.

But how do I find out what object is moving with Debug.Log?

Log the name of the GameObject you are translating. Also you can pass the GameObject as a second parameter to Debug.Log. if you do you will be able to click the log message and unity will highlight that object in the scene hierarchy.

It looks like it thinks “targ” is the First Person Player. What now

Now you figure out why and fix it. Seems like your raycast is hitting your player and you aren’t expecting that. Why? What’s your layermask set to and is the player’s layer part of it?

I’m so bad at Unity I don’t know what any of that even means…

If you want to quickly ignore the player in the Raycast (aside from using layers and layermasks) you can just set collider.enabled false directly before the raycast, then set back to true right after.

One problem I’m seeing is a malformed if statement here. Sort this out:

               if (hit.transform.CompareTag("Selectable")) Debug.Log("Object is selectable");
                {
                    Debug.Log("If statement successful");
                    PickUpFunction(hit.transform);
                }

For some reason when I copy and pasted the code, some of it was indented. I can try and paste it again

using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;

public class PickUp1 : MonoBehaviour
{

    public static bool IngredientCollected1 = false;





    void Update()
    {
        RaycastHit hit;

        if (Input.GetKeyDown("e"))
        {
           
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))   
            {
                Debug.Log("Raycast Hit!");
                if (hit.transform.CompareTag("Selectable")) Debug.Log("Object is selectable");
                {
                    Debug.Log("If statement successful");
                    PickUpFunction(hit.collider.gameObject.transform);
                }
            }
        }
    }
       
        public void PickUpFunction(Transform targ)
    {

        var rb = targ.GetComponent<Rigidbody>();
        var rend = targ.GetComponent<MeshRenderer>();
        var col = targ.GetComponent<BoxCollider>();
        if (rb != null) rb.useGravity = false;
        IngredientCollected1 = true;
        targ.position += new Vector3(0, 1000, 0); Debug.Log(targ + "is the gameobject");
        Debug.Log("object moved");
    }
}

Yeah it’s not the indentation that is the problem. You have this:

                if (hit.transform.CompareTag("Selectable")) Debug.Log("Object is selectable");
                {
                    Debug.Log("If statement successful");
                    PickUpFunction(hit.collider.gameObject.transform);
                }

When it should be this:

                if (hit.transform.CompareTag("Selectable"))
                {
                    Debug.Log("Object is selectable");
                    Debug.Log("If statement successful");
                    PickUpFunction(hit.collider.gameObject.transform);
                }

a += b is just shorthand for a = a + b. E.g.:

int a = 10;
a += 5; // a = a + 5
float b = 7f;
b -= 3.5f; // b = b - 3.5f

Oh yeah. I did that because I wanted to try and make sure the if statement was running but now that i think about it that was useless…