Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by peteynunchucks · Jul 23, 2021 at 05:07 AM · sprite renderer

Questions about sprite renderers.

Hello,

I am working on a pong-like game where when the ball hits one of the walls it changes color and only the same colored paddle can hit it. To do this, I have a script on the ball and one of the walls. The Wall script is able to access the ball script and change the color of the ball. To do this, I made the balls sprite renderer public in its script.

Since the sprite renderer has to be public for the wall script to access it, I have to attach a sprite renderer object to the script. This is where I have run into issues and was looking for any resources or tutorials to help.

Thank You

Comment
Add comment
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

2 Replies

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

Answer by andrew-lukasik · Jul 23, 2021 at 10:54 AM

(...) Since the sprite renderer has to be public for the wall script to access it (...)

Excuse me, partner, but what are you talking about? Your post communicates a considerable amount of confusion about how this code should work here. All components (on gameObjects) are open access as long as you obtain a reference to a given gameObject (on collision events, for example). I.e. you don't need public class field to do this. Check this out. There is this event OnCollisionEnter2D so make sure your ball object has a Rigidbody2D and a collider because then you can add a script like this one to your ball:

MyBalls.cs

 using UnityEngine;
 public class MyBalls : MonoBehaviour
 {
     void OnCollisionEnter2D ( Collision2D col )
     {
         var otherSpriteRenderer = col.gameObject.GetComponent<SpriteRenderer>();
         otherSpriteRenderer.color = Color.red;
     }
 }

OR

If you want a bit something more fancy than that, try

MyBalls.cs

 using UnityEngine;
 public class MyBalls : MonoBehaviour
 {
     // no code in this case, this component will be a kind of tag so other scripts can recognize it
 }

MyDestructibleWall.cs

 using UnityEngine;
 public class MyDestructibleWall : MonoBehaviour
 {
     int _hits = 0;
     [SerializeField] int _hitsToDestroy = 3;
     [SerializeField] Color _damageColor = new Color( 0.5f , 0 , 0 );
     void OnCollisionEnter2D ( Collision2D col )
     {
         var otherGameObject = col.collider.gameObject;
         var wasItABallTho = otherGameObject.GetComponent<MyBalls>();
         if( wasItABallTho!=null )
         {
             _hits++;
             
             Debug.Log( "Oh my! "+otherGameObject.name+" hit with me hard for the "+_hits+" time D: !" , otherGameObject );
 
             if( _hits==_hitsToDestroy )
             {
                 Destroy( this.gameObject );
             }
             else
             {
                 var thisSpriteRenderer = this.gameObject.GetComponent<SpriteRenderer>();
                 thisSpriteRenderer.color = Color.Lerp( Color.white , _damageColor , (float)_hits/(float)_hitsToDestroy );
             }
         }
     }
 }
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 peteynunchucks · Jul 28, 2021 at 05:59 AM

Hey @andrew-lukasik,

Thank you for the response and for clearing up the questions I had about accessing sprite renderers. I have been trying to figure out how to make the ball go back and forth between the color red and blue whenever it hits the wall with no luck. So far I have tried putting a else if statement together with an extra condition to make sure that when the ball is red and not null, that it changes the color to blue. Would a loop be better in this situation? Switch statement? Any guidance you can give is appreciated.

         if (Ballconfirmation !=null)
         {
             otherSpriteRenderer.color = Color.red;
         }
         else if (otherSpriteRenderer.color == Color.red  && Ballconfirmation !=null)
         {
             otherSpriteRenderer.color = Color.blue;
         }


Comment
Add comment · Show 1 · 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 andrew-lukasik · Jul 28, 2021 at 06:20 AM 0
Share

Try to code ball behavior in its own script that will be easy to find again. You won't remember where you put it one month from now.

 using UnityEngine;
 public class MyBalls : MonoBehaviour
 {
     [SerializeField] SpriteRenderer _spriteRenderer = null;
     [SerializeField] Color[] _colors = new Color[]{ Color.red , Color.blue };
     int _numHits = 0;
     void OnCollisionEnter2D ()
     {
         _numHits++;
         _spriteRenderer.color = _colors[ _numHits%_colors.Length ];
     }
 }

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

124 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

Related Questions

Sprite.Create alpha 1 Answer

I cannot see my sprites in the scene view of the Unity editor,Why can't I see my sprites in the scene view? Unity 5 0 Answers

How to make a customizable player's animations? 0 Answers

Work with sorite quality. How to make it less pixeled? 0 Answers

Diffeerence between Renderer's colors sensitivity 0 Answers


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