Changing the player's size during run-time

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

    }

??

Same thing you’d do. A script on which object is trying to buff the player, inside which type of function/method?

void OnTriggerEnter2D(Collider2D target)
    {
        if (target.tag == "ball")
        {
            Destroy(gameObject);
            Debug.Log("Collected :)");
            GetComponent<MovePlayer1>().transform.localScale += new Vector3(5f, 0);

        }

    }

This is the code I tried

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.

It didn’t help…

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 :)");

i need a check of understanding. answer the following questions:

Q1) what do you understand with this line?

if (target.GetComponent<MovePlayer1>().tag == "ball") {

Q2) is the object with the MovePlayer1 component tagged “ball”?

Q1) if the tag from the component of the player, which gets the reference from the target equals ball

Q2) no, it is tagged as player

I know it doesn’t work and that is my question, how i can refer to my player from this script ?

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”

//in OnTriggerEnter
if(null != target.GetComponent<MovePlayer1>()){
    //apply buff
}

and you didn’t need to change the way you were applying your buff from before.

I’m not sure if it’s actually an issue but you’re using Vector3 and supplying it with only two parameters.

if (target.tag == "ball") {
            Destroy(gameObject);
            Debug.Log("Collected_Buff");
        }
        if(null!=target.GetComponent<MovePlayer1>())
            GetComponent<MovePlayer1>().transform.localScale += new Vector3(5f, 0);

Any fixes here? The buff doesn’t work yet(the code is inside ONTrigger)

3026323--226240--upload_2017-4-9_13-49-6.png

 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

if(target.tag == "ball" || null != target.GetComponent<MovePlayer1>()){
    Destroy(gameObject);
   target.transform. += new Vector3(1f, 1f);
}

alternatively you can check for the player tag if you are not keen on checking for the component

if(target.tag == "ball" || target.tag == "Player"){
    Destroy(gameObject);
   target.transform. += new Vector3(1f, 1f);
}

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.

3026355--226242--upload_2017-4-9_14-46-52.png

As with before, show what you did to the script.

While I admit I made a typo in my previous reply, I believe it is something you would have been able to solve.

No I did not edit my previous reply, and I will not be pointing it out for you. I expect you to be able to spot the typo for yourself.

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