Home > Enterprise >  can i move a gameObject based on just its name?
can i move a gameObject based on just its name?

Time:12-19

Im using a raycast, putting its RaycastHit into a variable called raycastHit, using that to get the raycastHit.transform.name, could i then use the information from the name of the gameObject to move its position? Or if not, could i get the information from the raycastHit to instead move it straight away? (outside of the function of the raycast) I already looked for an answer, but ive been only finding results on how to move any gameObject.

EDIT1: my current version of the void:

public RaycastHit raycastHit;
    private void HandleHookShotStart()
    {
        
        if (TestInputDownHookShot())
        {
            int gm = LayerMask.NameToLayer("Ground");
            int lwm = LayerMask.NameToLayer("LightWeight");
            int layers = 1 << gm | 1 << lwm;
            if (Physics.Raycast(Shoulder.transform.position, cam.transform.forward, out raycastHit, float.PositiveInfinity, layers))
            {
                //Hit Something
                debugHitpointTransform.position = raycastHit.point;
                hookshotPosition = raycastHit.point;
                hookShotSize = 0f;
                HookShotTransform.gameObject.SetActive(true);
                HookShotTransform.localScale = Vector3.zero;

                if (raycastHit.collider.gameObject.layer == gm)
                {
                    Debug.Log("ground");
                    hitType = HitType.Ground;
                }
                else if (raycastHit.collider.gameObject.layer == lwm)
                {
                    //Debug.Log("lightweight");
                    hitType = HitType.Lightweight;
                    goToMove = raycastHit.collider.gameObject;
                    string raycastHitObjectname = goToMove.name;
                }

                state = State.HookShotThrown;
            }
        }

        
    }

EDIT2:

private void HandleHookShotGrab()
    {
        GameObject raycastHitObject = GameObject.Find(raycastHitObjectName);
    }

error is on raycastHitObjectName error:

CS0103 the name 'raycastHitObjectName' does not exist in the current context

CodePudding user response:

You dont need to find your gameobject by name, you can add a ref to the gameobject in the script from which you would be moving the gameobject from.

public RaycastHit raycastHit; 
public GameObject goToMove;//drag in to your gameObject component in unity editor

private void HandleRayCast()
{
    if (Physics.Raycast(transform.position, cam.transform.forward, out raycastHit))
    {
        //Hit Something
        goToMove.transform.position = raycastHit.point; 
    }
}

If you still need to find the gameObject by name, you can still get in the Start phase.

void Start() {
    goToMove = GameObject.Find("yourGameObjectName");
}

Don use GameObject.Find in Updates. Use it to keep a reference to your gameObjects at the initialization phase of your Monobehaviours. In small games or playaround projects its not a big deal, but it is an "expensive" command, so needs to be used with care.

  • Related