-1

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
  • 2
    `raycastHit.transform.position = ....` ? Maybe I don't get your question but if you already have a `Transform` reference .. what hinders you from moving the object? – derHugo Dec 18 '21 at 12:17
  • 1
    oh, to move the gameObject that was hit that makes total sense – rustyBucketBay Dec 18 '21 at 12:30

1 Answers1

1

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.

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • i want to access the ```gameObject``` the raycast hits outside of the raycast function to move it. I first did ```goToMove = raycastHit.collider.gameObject;``` to make goToMove the object the raycast hit, but because it doesnt let me reference that outside of the function, im trying to get the name of it, so then, outside of the function, ill be able to get the gameObject from its name. So ```string raycastHitObjectname = goToMove.name;```. Now how would i be able to get manipulate the gameobject using that outside the raycast funtion? – TerazikMubaloo Dec 18 '21 at 12:58
  • i just tried ```GameObject raycastHitObject = GameObject.Find(raycastHitObjectName);```, but it says ```The name 'raycastHitObjectName' does not exist in the current context``` – TerazikMubaloo Dec 18 '21 at 13:01
  • 1
    note that `raycastHit` is a public class variable passed by reference with the `out` keyword. The value to the variable is "filled in" when the `HandleRayCast` is executed, so if the variable is `public` you can access it from anywhere in the code, and if its `pravate` you can access it from any method within the class – rustyBucketBay Dec 18 '21 at 13:10
  • 1
    you can have a look to [this](https://stackoverflow.com/questions/70380238/is-there-a-way-to-reference-a-raycasthit-of-a-raycast-outside-of-the-void-the-ra/70380617#70380617) – rustyBucketBay Dec 18 '21 at 13:11
  • i already have that, but it still says the error. (i edited the question to what my raycast looks like now, so maybe i added something that stops it from being accessible from outside) – TerazikMubaloo Dec 18 '21 at 13:35
  • 1
    waht error do you get and in which line? does the raycast actually hit? – rustyBucketBay Dec 18 '21 at 13:45
  • the raycast definitely hits. I added the line + error i got on it to the question – TerazikMubaloo Dec 18 '21 at 14:01
  • 1
    if the name is what you use (`string raycastHitObjectname = goToMove.name;`) it is the string what you need to make public. Make your `string raycastHitObjectname;` a public class variable – rustyBucketBay Dec 18 '21 at 14:10
  • 1
    however it is better to use a referece to the hit `GameObject` instead of keeping the name and finding after the name of the gameoObject by string in the scene when you need to set the position – rustyBucketBay Dec 18 '21 at 14:11