- Home /
i done this b4 but forgot :( touch screen
im sure all you know what this question is going to be lol, sorry for repeating. Anyone kind enuf to give a quick reminder as to why the code runs when touching anywhere on the screen? it shud run when i touch the gameobject with the collider=="Button"
function Update ()
{
if( Input.touchCount > 0 )
{
var touch = Input.GetTouch(0); // Cache touches for better performance
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay( touch.position );
// B E G A N
if( touch.phase == TouchPhase.Began )
{
if( Physics.Raycast( ray, hit, 50 ) ) // If theres a hit
{
if( hit.collider.tag == "Button" ) // If the hit is with button collider
{
//be happy
}
}
}
}
Answer by AtomicMarine · May 01, 2012 at 02:04 PM
I did a little check up on your code, assuming your tagged object is a gameObject (which it should be) then this should work I just broke up the act of finding the tagged object and the act of returning weather it has been hit or not for elegance and hopefully to get the code working. let me know.
function Update ()
{
if( Input.touchCount > 0 )
{
var touch = Input.GetTouch(0); // Cache touches for better performance
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay( touch.position );
// B E G A N
if( touch.phase == TouchPhase.Began )
{
if( Physics.Raycast( ray, hit, 100 ) ) // If theres a hit
{
var obj1 : GameObject = GameObject.FindWithTag("Button"); // Break up the task of finding the button and hitting it in two
if( hit.transform == obj1.transform ) // If the hit is with button collider
{
//be happy
}
}
}
}
@samz: stop spam$$anonymous$$g such posts as answers. Use comments!
one second. i got 2 objects. when touched they are deleted. problem is you always have to touch object1 and delete before you can touch and delete object2 :S whats the deal with that? i want the script to delete which ever object is touched
function Update ()
{
if( Input.touchCount > 0 )
{
var touch = Input.GetTouch(0); // Cache touches for better performance
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay( touch.position );
if( Physics.Raycast( ray, hit, 100 ) ) // If theres a hit
{
var obj1 : GameObject = GameObject.FindWithTag("Button"); // Break up the task of finding the button and hitting it in two
if( hit.transform == obj1.transform ) // If the hit is with button collider
{
// B E G A N
if( touch.phase == TouchPhase.Began )
{
Destroy(obj1.gameObject);
}
}
}
}
}
Answer by Seth-Bergman · May 01, 2012 at 05:22 PM
Using GameObject.FindWithTag is returning the first guy it finds, you could use FindGameObjectsWithTag, which will return a list of all objects with that tag:
var objects = GameObject.FindGameObjectsWithTag("Button");
then use
for (var object in objects)
{
if( hit.transform == object.transform ){ // If the hit is with button collider
//etc...
}
}
Your answer
Follow this Question
Related Questions
I am making android fps and my raycast shooting button don't work. What I should do? 1 Answer
NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer
Button Focus Prevents Touch Raycast (Android C#) 1 Answer
Trouble with Ray & Raycasting and AddForce() 1 Answer
Button Navigation none - not working with touchscreen 0 Answers