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
Answer by andrew-lukasik · Jul 23, 2021 at 10:54 AM
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:(...) Since the sprite renderer has to be public for the wall script to access it (...)
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 );
}
}
}
}
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;
}
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
Follow this Question
Related Questions
Sprite.Create alpha 1 Answer
How to make a customizable player's animations? 0 Answers
Work with sorite quality. How to make it less pixeled? 0 Answers