Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Russellcart · Jan 07, 2019 at 09:20 AM · gameobjectmobiletouch

Rhythm Game - Not all notes on touch are changing sprite. (easy to read)

  1. So when my items fall and are needing to be touched by the user, not all of them change sprite on touch on mobile. - - -

  2. This code is on all the touchable objects that fall.


  3. Most do but maybe 30% of them do not. - - -


  4. Also in the console it throws up the error on touch - - -


  5. NullReferenceException: Object reference not set to an instance of an object ButtonCollider.Update () (at Assets/Scripts/ButtonCollider.cs:67) (The line ---- touchedObject = hit.information...)


  6. The gameobject variable 'touchedobject' is created at the start of the script. --


  7. Also sometimes more than one note will change on the screen after touching another even though it hasn't been touched. Why is that?


    1. 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;
                 }
 
             }
 
         }
 
     }
 }

 

 
 

























Comment
Add comment · Show 14
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Russellcart · Jan 07, 2019 at 10:05 AM 0
Share

Anyone? Ive been stuck on the for days.

avatar image NorthStar79 · Jan 07, 2019 at 10:57 AM 1
Share

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.

avatar image Russellcart NorthStar79 · Jan 07, 2019 at 11:22 AM 0
Share

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!

avatar image NorthStar79 Russellcart · Jan 07, 2019 at 11:29 AM 0
Share

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.

Show more comments
avatar image Russellcart · Jan 07, 2019 at 01:44 PM 0
Share

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.. :( :( :(

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Carterryan1990 · Jan 07, 2019 at 02:18 PM 0
Share

Also Im not sure why you are using a forloop? Seems a bit strange.

avatar image Carterryan1990 · Jan 07, 2019 at 02:21 PM 0
Share
  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;
         }
avatar image Russellcart Carterryan1990 · Jan 07, 2019 at 02:25 PM 0
Share

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);
                     }
 
 
                 }
             }


avatar image Russellcart · Jan 07, 2019 at 02:28 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

207 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rhythm Game - When I touch an object with mobile in only activates 80% of the time... 1 Answer

How do I move an object with my finger?[C#] 1 Answer

move 3d object with left and right ui buttons 0 Answers

Unity's conversion flow from mouse to touch? 0 Answers

access gameobject via touch 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges