- Home /
Rhythm Game - Not all notes on touch are changing sprite. (easy to read)
So when my items fall and are needing to be touched by the user, not all of them change sprite on touch on mobile. - - -
This code is on all the touchable objects that fall.
Most do but maybe 30% of them do not. - - -
Also in the console it throws up the error on touch - - -
NullReferenceException: Object reference not set to an instance of an object ButtonCollider.Update () (at Assets/Scripts/ButtonCollider.cs:67) (The line ---- touchedObject = hit.information...)
The gameobject variable 'touchedobject' is created at the start of the script. --
Also sometimes more than one note will change on the screen after touching another even though it hasn't been touched. Why is that?
IN THE console the debug log is seeing that its being hit its just not changing sprite or if I add set active false it does not do this. Is it getting confused due to the fast tapping?
using System.Collections; using System.Collections.Generic; using UnityEngine; <<<<< Ignore this.
void Update()
{
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit2D hitInformation = Physics2D.Raycast(ray.origin, ray.direction);
hitInformation.transform.gameObject.GetComponent<SpriteRenderer>().sprite = blank;
}
}
}
}
}
Hello, this answer will be a blind shot. but, can you just change your Update () to FixedUpdate() and tell us if it solves your problem.
if it works, I will explain why, if it not, well we can try something else.
I did a lot of things. $$anonymous$$ade each note spawn on the layer below the last to avoid touching errors. I moved the recast script onto the camera ins$$anonymous$$d of the notes.
AND I tried your method and it has worked. Im pretty sure it was your method that helped.
Jesus I feel like I just won't the lottery, finally it works!
Why is fixed update better?
Thank you so so so sosodkoskdoinsdjifbniosdnf much!
well, $$anonymous$$y method worked because you are using the Physics system that works at fixed time steps, but checking if TouchPhase.Began in normal Update(). when your frame rate drastically changes from your fixed updates that cause this type of errors.
It is highly recommended that NOT use Raycast operations in Update. especially in frame dependent situations.
I am glad that this answer helped.
I even just rebuild it all on another unity project to see if there was anything wrong with that project. It still does it, using the same code. So it is the code somewhere. I also just set it to destroy on touch. Again some DONT destroy but play the debug to let me know its been touched.. :( :( :(
Answer by Russellcart · Jan 07, 2019 at 02:55 PM
So I made it better. It worked fine before without doing this but it seems to work now. If anyone who reads this can explain why it would be great.
I did some research and went into Edit >> Project settings >> time >> and lowered the value of 'Fixed Timestep'. I believe this allows more frames and has allowed it to work.. for now.
Answer by Carterryan1990 · Jan 07, 2019 at 02:17 PM
Im not sure if this is the issue but you should check if you are hitting anything. Try " if(hitinformation.transform != null)" //then do your stuff.Also If you are getting errors that's why your code isn't executing all of the time. Try that and let me know if it helps.
Also Im not sure why you are using a forloop? Seems a bit strange.
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit2D hitInformation = Physics2D.Raycast(ray.origin, ray.direction);
if (hitInformation.transform != null && hitInformation.transform.GetComponent<SpriteRenderer>())
hitInformation.transform.gameObject.GetComponent<SpriteRenderer>().sprite = blank;
}
Hmm I am using touch on a mobile. $$anonymous$$ouse down isn't suitable as at times you need to press more than one thing at once. $$anonymous$$ouse down only allows for one press at a time if I'm not mistaken.
void FixedUpdate()
{
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit2D hitInformation = Physics2D.Raycast(ray.origin, ray.direction);
if (hitInformation.transform != null)
{
Destroy(hitInformation.transform.gameObject);
touches = touches + 1;
Debug.Log("Number of touches so far is" + touches);
}
}
}
Also I coded in a Debug, the debug appears to let me know the object has been hit. But the code such as Destroy or change sprite, whichever I use doesn't activate. Very strange.