Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

prefab localscale

Discussion in 'Scripting' started by spearbeard, May 7, 2018.

  1. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    I have a pickup in my game that transforms the scale of my player object when it collides with it. the problem I am having is that when ever i make duplicates/childs of the parent object (pickup) the local scale transform does not replicate over onto the other pickups there for when the player collides with them it gets destroyed but the player does not get scaled again.

    Code (CSharp):
    1. public class PickupCollsion : MonoBehaviour {
    2.  
    3.    void OnTriggerEnter( Collider other)
    4.     {
    5.         if(other.tag == "Player") // if the tag of the gameobject is player the pickup gets removed
    6.         {
    7.             Vector3 newScale = transform.localScale; // sets newscale for the player
    8.             newScale *= 1.2f; //new scale value for the player
    9.             other.transform.localScale = new Vector3(1.2f, 1.2f, 1.2f); // the players x, y, z values will gbe increased by these values
    10.             Destroy(gameObject); //Pickup gameobject gets destroyed on collision
    11.  
    12.         }
    13.     }
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    You've created the newScale Vector3 but you're not using it to scale the player. "transform.localScale" is getting the scale of your pickup object not the player. Use "other.transform.localScale" to get the players scale and then set the scale of the player to newScale and your player object will scale up each time it collides with another pickup object. Also you should use other.CompareTag instead of other.tag for optimization purposes.

    Code (CSharp):
    1. if(other.CompareTag ("Player")) // if the tag of the gameobject is player the pickup gets removed
    2. {
    3.     Vector3 newScale = other.transform.localScale; // sets newscale for the player
    4.     newScale *= 1.2f; //new scale value for the player
    5.     other.transform.localScale = newScale; // the players x, y, z values will gbe increased by these values
    6.     Destroy(gameObject); //Pickup gameobject gets destroyed on collision
    7.  
    8. }
     
  3. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    THANK YOU. that has sorted it out.