My, player, a wide cube that goes left right must use a bouncing ball to collect pick-ups, and I plan to make some of them buff / nerf for the ball / the player. Now I need help with buff for the player. I want to make a script for the collectable that makes the player’s x scale bigger. However I can’t get the reference to the player’s scale although I did manage to do it with the ball
Here is the Buff_Size for the ball Script(From the collectable):
void OnTriggerEnter2D(Collider2D target)
{
if (target.tag == "ball")
{
Destroy(gameObject);
Debug.Log("Collected :)");
// Target is the ball.
target.transform.localScale += new Vector3(1f, 1f);
}
}
The OnTriggerEnter2D method of your buff script checks for the tag “ball” before applying the buff. you should either remove the tag check which means the buff will affect anything it triggers on, or add a tag check for the box player in your condition.
While that is unfortunate, it would help whoever is trying to help you greatly if you provided what you tried, and/or what the unintended result you got was.
if (target.GetComponent<MovePlayer1>().tag == "ball") {
Destroy(gameObject);
GetComponent<MovePlayer1>().transform.localScale += new Vector3(5f, 0);
Debug.Log("Collected :)");
}
// Another attempt. This is without the if condition
Destroy(gameObject);
GetComponent<MovePlayer1>().transform.localScale += new Vector3(5f, 0);
Debug.Log("Collected :)");
you can always check by component instead if your player has a specific component no other object has. What I don’t understand is your insistence to check against the tag “Ball”
if(null!=target.GetComponent<MovePlayer1>())
GetComponent<MovePlayer1>().transform.localScale += new Vector3(5f, 0);
what you are saying here is, if target has the component “MovePlayer1”, check for this(the buff) object’s “MovePlayer1” component, get the transform and add to this(the buff) object’s scale. Obviously not what you want.
You can use “||” to have either condition run the if-statement
Just remember checking for tags is case sensitive, so using lowercase letters when uppercase letters are needed, or having an extra space, will cause the check to fail.
Since there was a lot of discussion since my previous reply, I just wanted to point out that often in debugging issues like this it’s helpful to know which object has the script attached. Often, it can be guessed, but since this is a sort of debugging, it’s nicer to be certain.