- Home /
Lines of code playing multiple times?
Hi,
(Wasn't sure what to name this. ) For a phone game, I'm using Unity's ray casting features to allow me to run scripts when Objects with Colliders are touched. I have this feature working.
But I've ran into a problem when trying to make a script that, if you tap on an object it will cycle through an array of textures. It works sort of but every time I tap the object it runs through 5 textures, not 1 (The number it should be doing per tap). I know it's doing this because I added a debug.log script as well and it tells me that I've apparently tapped the object 5 times. Which is the problem.
I don't think it has anything to do with my TouchLogicFixed script (The script that my TextureChange Script is inheriting from) because I tested the debug.log on other objects and they tell me I've tapped them once. It must be to do with my TextureChange Script below.
using UnityEngine;
using System.Collections;
public class NewTex : TouchLogicFixed // The Script that this Script is inheriting form.
{
public Texture[] Level = new Texture[10];
int Current = 0;
public GameObject LevelChoose; // I know this isn't necessary since this script is on this object, but it won't be later.
void Began() //On my TouchLogicFixed Script, when a touch begins on an object it broadcasts this "Began" function for this object to use.
{
Current = Current + 1; //This line seems to be executed 5 times.
Debug.Log("TapBegan"); //This line seems to be executed 5 times.
}
void Ended() //On my TouchLogicFixed Script, when a touch ends on an object it broadcasts this "Ended" function for this object to use.
{
LevelChoose.renderer.material.mainTexture = Level[Current]; //This line seems to be executed 5 times.
Debug.Log("TapEnd"); //This line seems to be executed 5 times.
}
}
Below is the TouchLogicFixed script where Began() and Ended() are called.
using UnityEngine;
using System.Collections;
public class TouchLogicFixed : MonoBehaviour
{
public static int currTouch = 0;//so other scripts can know what touch is currently on screen
private Ray ray;//this will be the ray that we cast from our touch into the scene
private RaycastHit rayHitInfo = new RaycastHit();//return the info of the object that was hit by the ray
void Update ()
{
//is there a touch on screen?
if(Input.touches.Length <= 0)
{
//if no touches put code here if needed
}
else //if there is a touch
{
//loop through all the the touches on screen
for(int i = 0; i < Input.touchCount; i++)
{
currTouch = i;
//this is for 3d objects with colliders
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);//creates ray from screen point position
if(Physics.Raycast(ray, out rayHitInfo))
{
rayHitInfo.transform.gameObject.SendMessage("Began");
}
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);//creates ray from screen point position
if(Physics.Raycast(ray, out rayHitInfo))
{
//Camera.mainCamera.backgroundColor = Color.gray;//test to show it works
rayHitInfo.transform.gameObject.SendMessage("Ended");
}
}
}
}
}
}
Well, since we've no way of knowing how Began() and Ended() are called it's impossible to make any suggestions.
@Graham Dunnett - He's using Send$$anonymous$$essage(): line 36 and 51 of the second script.
@Jack62Lewis - I don't spot anything wrong. Any chance you have (mistakenly) attached this script to more than one object?
How many instances of the TextureChange script do you have attached to rayHitInfo.transform.gameObject
?
How many scripts in the scene that inherit from TouchLogic? All the inherited classes will inherit the Uodate() so you could get multiple messages that way if there are multiple children
Answer by Peter G · Jul 28, 2013 at 09:07 PM
You just need to add an extra wrapper around the message.
if(Physics.Raycast(ray, out rayHitInfo))
{
if(rayHitInfo.transform == transform)
SendMessage("Began");
//or SendMessage("Ended")
// You need to add the check in both sections.
}
Just another possible solution. You have basically created a touch handler already. Maybe you remove all the inheritance then just have TouchLogic
send messages wherever they need to go. Essentially TouchLogic
could be the event handler/distribution class. If you wanted to try that you would ignore my first correction then simply remove the inheritance in all your classes (and add back in the inheritance from MonoBehavior
).
Your answer
Follow this Question
Related Questions
Load Material to GameObject 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Select colour then change texture 0 Answers
simple texture question 1 Answer
Efficiency of Game Loops 2 Answers