- Home /
problem with the change's speed between 2 texture using a switch script, how can i speed it up?
hello everyone and thanks for the help. I made this switch script that switch between 2 characters (both childs of an empty object) by touching the right half of the screen. My problem is that if i touch repeatedly the right half of the screen, the script will be able to keep up, but it wont change the texture. I'll use an example to explain me better, lets say i have a red and a green circles as my 2 characters. If i touch too fast the screen it'll change between the green and the red one but in the game mode it will take something like 1 second to change the texture from red to green or viceversa, so it makes the game confusing (even if it works). Here is my code:
int attivo = 1;
public void Update()
{
foreach(Touch touch in Input.touches)
{
if(touch.position.x > Screen.width / 2 && touch.phase == TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject())
{
switch (attivo)
{
case 1:
attivo = 2;
rosso.gameObject.SetActive(false);
verde.gameObject.SetActive(true);
break;
case 2:
attivo = 1;
verde.gameObject.SetActive(false);
rosso.gameObject.SetActive(true);
break;
}
}
}
}
how can i do that?
Answer by jmfp92 · Nov 09, 2021 at 09:36 AM
Instead of switching game objects at runtime why not just change the mesh in the mesh renderer or the sprite in the sprite renderer? It would be much faster and certainly more efficient than this. Hope that helps
ty for your answer, im a unity rookie, can you explain me how to do it?
Not a problem, we've all been there. I'm certainly not a pro either. It depends on if you are using a 2d or 3d environment. Assu$$anonymous$$g that it is 2D because of the use of touch functions you would just do something like this
//attach this script to the gameobject with the spriterenderer which is
//most likely your player object
SpriteRenderer spriteRen;
Sprite firstSprite, secondSprite;
void SwitchSprite(){
if(spriteRen.sprite == firstSprite){
spriteRen.sprite = secondSprite;
}else{
spriteRen.sprite = firstSprite;
}
Then you call the function SwitchSprite() in your update when the touch is in the correct position
Your answer
Follow this Question
Related Questions
How can I stop input from registering inside my game when I am dragging status bar from the top? 0 Answers
Things to know about 2D games! 1 Answer
Help with the new Mobile Notification System in Unity 1 Answer
how i can import game source code to unity? 1 Answer
My script only partially works 2 Answers