- Home /
raycast hit doesn't seem to work
hi, i want to compare the name of a prefab that gets instantiated and print a something, but the condition doesn't seem to work.need help in this.
using UnityEngine;
using System.Collections;
public class prefabCreation_1 : MonoBehaviour {
//public GameObject startbutton;
public GameObject button;
bool onGameMode = true;
void Update()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(onGameMode == true)
{
Instantiate(button, transform.position,transform.rotation);
//prefabCreation();
}
if (Physics.Raycast (ray, out hit, 100))
{
if(Input.GetMouseButtonUp(0))
{
if(hit.collider.gameObject.name == "startButton")
{
print ("working");
}
}
}
onGameMode = false;
}
Answer by sumeeton · Mar 31, 2015 at 11:39 AM
Why did you include this check
if(Input.GetMouseButtonUp(0))
Remove it. You should check it before raycast.
Also, try printing the name of the object in the console. That will help you to find which collider it hits.
its working now. i used
if(hit.collider.tag == "cardPrefab")
its working now. but still dont know why the previous isn't.
Answer by suchoparek · Mar 31, 2015 at 12:50 PM
You don't have to put Physics.Raycast in if statement.
Physics.Raycast (ray, out hit, 100);
if(Input.GetMouseButtonUp(0))
{
if(hit.collider.gameObject.name == "startButton")
{
print ("working");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613191748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make door open and close with Input.GetButtonDown? 1 Answer
How to check if variable defined in script is present in shader? 1 Answer
Importing a class into multiple scripts 0 Answers
how can I change a light with multiple triggers. ? 0 Answers
Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers