0

So I'm trying to save the object clicked on by the mouse to a seperate variable, but the RaycastHit isn't converting to GameObject, even in an if statement checking its type.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Select : MonoBehaviour
{
    public GameObject selectorPrefab;

    private GameObject selectedObject;
    private GameObject clone;

    void Update()
    {
        if(Input.GetMouseButtonDown(0))//left click
        {
            if(clone)
            {
                Destroy(clone);
            }

            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Ship")
            {
                Vector3 position = hit.transform.position;
                float scaleMultiplier = (hit.transform.localScale.x + hit.transform.localScale.z) / 2;

                clone = Instantiate(selectorPrefab);
                clone.transform.position = position;
                clone.transform.localScale *= scaleMultiplier;

                if(hit is GameObject)//Green underline here
                {
                    selectedObject = hit;//Red underline under "hit"
                }
            }
        }
    }
}
4
  • 2
    You might want to add the relevant technology/API/framework tags. Commented Mar 4, 2017 at 0:42
  • selectedObject = hit.transform.gameObject Commented Mar 6, 2017 at 22:20
  • Your error message at the red underline is probably about not being able to assign from one type to another. You need to cast it, like selectedObject = (GameObject)hit; Commented Mar 6, 2017 at 22:22
  • really - if an object passes this is test I cant assign it to a variable of that type without a cast? Commented Mar 6, 2017 at 22:30

2 Answers 2

2

Just using the is operator doesn't change the compile-time type of the hit variable. You could either cast within your if body, or use as instead:

var hitGameObject = hit as GameObject
if (hitGameObject != null)
{
    selectedObject = hitGameObject;
}

In C# 7 you could introduce a new variable in the if statement instead:

if (hit is GameObject hitGameObject)
{
    selectedObject = hitGameObject;
}

But if you know that it will always be a GameObject (barring bugs), just cast:

// No if statement, just an unconditional cast
selectedObject = (GameObject) hit;
Sign up to request clarification or add additional context in comments.

Comments

1

The result of a raycast, RaycastHit, is not a GameObject; instead it contains a set of information about the detected hit which you can see listed here: https://docs.unity3d.com/ScriptReference/RaycastHit.html

The object you're looking for is hit.collider, which is a component of the hit GameObject that represents its volume in Physics-space; you can retrieve the overall GameObject with hit.collider.gameObject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.