- Home /
Question by
zombiewalker · May 31, 2012 at 12:04 PM ·
instantiateraycastprefab
Help~!! Raycast on an instantiated prefab can not work!!
Hi,
I've been working on a function: 1.User click on Button_A (object, not GUI button, using Raycast)(This function is done) 2.Then instantiate another object_B (This function is done) 3.Then user can click on Object_B to do something (not working)
This the code I am using:
if(Input.GetMouseButtonDown(0))
{
ray = camera.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if(hit.collider.name =="Button_A")
{
Instantiate(Object_B_Prefab, ObjectBPosition, Quaternion.identity);
}
if(hit.collider.name =="Object_B") print("Clicked");
}
}
What went with my code? Anyone could help...?
Thanks
Comment
Best Answer
Answer by flamy · May 31, 2012 at 12:30 PM
if(Input.GetMouseButtonDown(0))
{
ray = camera.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if(hit.collider.name =="Button_A")
{
var foo=Instantiate(Object_B_Prefab, ObjectBPosition, Quaternion.identity);
foo.name="Object_B";
}
if(hit.collider.name =="Object_B") print("Clicked");
}
}
change the code this way, it will work. The reason is when you instantiate the name will be "Object_B(clone)" so your if loop will never get satisfied. also you can check like
if(hit.colloder.name.contains("Object_B"))
even this works.