Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object changes size when picking up

Discussion in 'Scripting' started by BT723, Jul 14, 2019.

  1. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    I created a script for picking up an object(cube) and whenever I do this it changes scale and I am not sure why or how to fix this.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class pickup : MonoBehaviour
    6. {
    7.     public Transform theDest;
    8.  
    9.     void OnMouseDown()
    10.     {
    11.         GetComponent<BoxCollider>().enabled = false;
    12.         GetComponent<Rigidbody>().useGravity = false;
    13.         GetComponent<Rigidbody>().freezeRotation = false;
    14.         this.transform.position = theDest.position;
    15.         this.transform.parent = GameObject.Find("Tempparent").transform;
    16.     }
    17.     void OnMouseUp()
    18.     {
    19.         this.transform.parent = null;
    20.         GetComponent<Rigidbody>().useGravity = true;
    21.         GetComponent<Rigidbody>().freezeRotation = true;
    22.         GetComponent<BoxCollider>().enabled = true;
    23.  
    24.     }
    25. }
    thanks
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    Deleted User likes this.
  3. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
  4. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    I changed my script but now it doesn't work instead the cube just flys away and has no gravity when I test it
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class pickup : MonoBehaviour
    6. {
    7.     public Transform theDest;
    8.     public GameObject player;
    9.     void OnMouseDown(Transform Tempparent)
    10.     {
    11.         GetComponent<BoxCollider>().enabled = false;
    12.         GetComponent<Rigidbody>().useGravity = false;
    13.         GetComponent<Rigidbody>().freezeRotation = false;
    14.         this.transform.position = theDest.position;
    15.         player.transform.SetParent(Tempparent);
    16.     }
    17.     void OnMouseUp(Transform Tempparent)
    18.     {
    19.         GetComponent<Rigidbody>().useGravity = true;
    20.         GetComponent<Rigidbody>().freezeRotation = true;
    21.         GetComponent<BoxCollider>().enabled = true;
    22.         player.transform.SetParent(Tempparent, false);
    23.     }
    24. }