Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

hi there im having a rought time with this code, trying to make a gameobject grow in scale from touching this one, and when a set amount of scale is reached destroy this game object ( the one who scales up the other), but my problem so far is that when i access the playerscript scale it dosnt add any scale to the gameobject.

using UnityEngine;
using System.Collections;

public class ScaleUp : MonoBehaviour {

    public GameObject Player;
    public PlayerMovement PlayerScript;

    private float playerScaleX;
    private float playerScaleY;

    public float scaleUp = 10.0f;

    void Start()
    {
        Player = GameObject.FindGameObjectWithTag ("Player");

        PlayerScript = Player.GetComponent <PlayerMovement>();

        playerScaleX = Player.transform.localScale.x;
        playerScaleY = Player.transform.localScale.y;
    }

    void Update ()
    {
    }

    void OnTriggerEnter2D (Collider2D other)
    {
        if (other.tag == "Player") 
        {
            playerScaleX += scaleUp;
            playerScaleY += scaleUp;

            Debug.Log ("GROW");
        }
    }
}
share|improve this question

2 Answers 2

You want to actually assign a new value to Player.transform.localScale then, not just change the local variables playerScaleX and playerScaleY.

Vector3 playerScale = Player.transform.localScale;
playerScale.x += scaleUp;
playerScale.y += scaleUp;
Player.transform.localScale = playerScale;

or, it you really want to use the existing variables playerScaleX and playerScaleY:

playerScaleX += scaleUp;
playerScaleY += scaleUp;
Vector3 playerScale = Player.transform.localScale;
playerScale.x = playerScaleX;
playerScale.y = playerScaleY;
Player.transform.localScale = playerScale;
share|improve this answer
    
i want to use the scale values of the object that is going to be scaled Up and then add a certain amount of scale, the variables playerScaleX and playerScaleY are for just storing the variables of that object. some like this: player enters the trigger then his scale when inside the trigger goes up until a certain scale, at the same time the trigger has a certain amount of scale to give, it counts until gives it all to the player, then the trigger is destroyed. –  Bomberclaw Jan 11 at 1:30

your main problem is that, the object Player.transform.localScale is a reference value. When you change it's value the change persist in the original place. But, Player.transform.localScale.x and Player.transform.localScale.y aren't a reference values. When you create a copy of those values in

playerScaleX
playerScaleY

and you modify then, Player.transform.localScale don't receive the modification. The result, your gameobject don't scale. You must do something as the Thelo's sample code.

BTW: If you retrieve Player and PlayerScript by code, you don't put the as public object. That is a good practice for Unity3D.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.