Hello, I’m trying to make a pong recreation and I have a collision prefab with a particle system on it that is instantiated whenever the ball hits an object. This works just fine, but what I’m trying to do is have the color of the particle system change when hitting specific objects (Goal, players, etc)
I’m trying to achieve this by having a setter method of the ball that sees what the tag is of the object that it collides with, and sets it as a variable. Then in the collision prefab, gets the string variable with the tag using a getter, and change the color of the particle system based on whatever the tag is. The issue is that when I try to get the string in the collision prefab, the string is returned null.
Here is the code for the ball prefab:
public class Ball : MonoBehaviour
{
Rigidbody2D rb;
public float x = 1.0f;
public GameObject collisionPoint;
public float speedIncrease;
public float maxSpeed = 50;
public string lastCollided;
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(x, (Random.Range(0, .9f)));
rb.rotation = Random.Range(1, 3);
}
void OnCollisionEnter2D(Collision2D collision)
{
setCollisionTag(collision);
//Debug.Log("Ball collisiont is: " + lastCollided);
if (collision.gameObject.tag.Equals("Walls") || collision.gameObject.tag.Equals("Player") || collision.gameObject.tag.Equals("Goal"))
{
ContactPoint2D contact = collision.contacts[0];
Instantiate(collisionPoint, new Vector2(contact.point.x, contact.point.y), transform.rotation);
}
if ((rb.velocity.x > (maxSpeed * -1) && rb.velocity.x < maxSpeed) && collision.gameObject.tag.Equals("Player"))
{
rb.velocity = new Vector2(rb.velocity.x + speedIncrease, rb.velocity.y);
}
}
private void setCollisionTag(Collision2D collision)
{
lastCollided = collision.gameObject.tag.ToString();
Debug.Log("Tag set is: " + lastCollided);
}
public string getCollisionTag()
{
return lastCollided;
}
}
Here is the code for the collision point prefab:
public class CollisionPoint : MonoBehaviour
{
public Ball ball;
public ParticleSystem ps;
string lastCollide;
Color32 player = new Color32(240, 84, 84, 255);
Color32 wall = new Color32(255, 255, 255, 255);
Color32 goal = new Color32(7, 154, 227, 255);
void Start()
{
ps = GetComponent<ParticleSystem>();
ParticleSystem.MainModule main = ps.main;
lastCollide = ball.getCollisionTag();
Debug.Log("Point tag got is: " + lastCollide);
if (lastCollide.Equals("Player"))
{
main.startColor = new ParticleSystem.MinMaxGradient(player);
} else if (lastCollide.Equals("Goal"))
{
main.startColor = new ParticleSystem.MinMaxGradient(goal);
} else
{
main.startColor = new ParticleSystem.MinMaxGradient(wall);
}
ps.Play();
}
private void Update()
{
if (!ps.isPlaying)
{
Destroy(gameObject);
}
}
}
You can see that in the collision point prefab and the setter of the ball, there is a Debug.Log to display what the tag is that was acquired. In the setter method, the proper tag is spit out, while in the collision point prefab the value is null, and only the “else” displays the color
Pic of experience:
The ball moves to the right and hits the paddle, the tag that is set in the setter is a player, but when using the getter method in the collision point, the tag that it gets doesn’t exist, thus only playing the default color.
So my question is:
Why is the getter not getting anything in the collision method? Is there a better or different way that we can get the tag that the ball collided with, and run the proper code based on that?