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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

When I Instantiate an object why does is it returned in a different scale? (and question for RB)

Discussion in 'Scripting' started by Hotpots, Oct 27, 2015.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey guys,
    So before in my prototype just using basic 3d cubes I was able to implement a pick up and drop system.
    However after importing final models I've tested my script once again.

    The item appears perfectly upon entering the level however after picking up the item and dropping it, the item has suddenly halved in size for some reason, and I'm totally stumped.

    I also have a small question regarding rigid bodies, when I apply a rigid body to my collectable items they just fall straight through my level even though the floor of my level is using a box collier (I had problems with the mesh collider so I just utilised the box collider :p)


    Object before pick up:


    Object after pick up:


    My ObjectPickup script below (attached to the player):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ObjectPickup : MonoBehaviour
    6. {
    7.     public Image bookimage;
    8.     public Image mapimage;
    9.  
    10.     public Text bookweightA;
    11.     public Text bookweightS;
    12.  
    13.     public Text mapweightA;
    14.     public Text mapweightS;
    15.  
    16.     public static float itemweight;
    17.  
    18.     bool isholding;
    19.     //float timeleft = 2;
    20.     Color visible = new Color(0f, 0f, 0f, 100f);
    21.     Color invisible = new Color(0f, 0f, 0f, 0f);
    22.  
    23.     GameObject book;
    24.     GameObject map;
    25.  
    26.     void Start()
    27.     {
    28.         book = GameObject.FindGameObjectWithTag("book");
    29.         map = GameObject.FindGameObjectWithTag("map");
    30.         //anim = GetComponent<Animator>();
    31.     }
    32.  
    33.  
    34.     void Update()
    35.     {
    36.         if (Input.GetKey("q") && isholding == true)
    37.         {
    38.             DropItem();
    39.         }
    40.     }
    41.  
    42.  
    43.     void OnTriggerStay(Collider other)
    44.     {
    45.         // If the entering collider is a book...
    46.         if (other.gameObject.tag == "book" && Input.GetKey("e"))
    47.         {
    48.             itemweight = 1.2f;
    49.             ScoreManager.currentweight += itemweight;
    50.             other.gameObject.SetActive(false);
    51.             bookimage.color = visible;
    52.             isholding = true;
    53.             Color c = bookweightA.color;
    54.             c.a = 100;
    55.             bookweightA.color = c;
    56.             bookweightA.CrossFadeAlpha(0, 2, false);
    57.             /*timeleft -= Time.deltaTime;
    58.             Debug.Log(timeleft);
    59.             if (timeleft <= 0)
    60.             {
    61.                 c.a = 0;
    62.                 bookweightA.color = c;
    63.                 timeleft = 0;
    64.             }*/
    65.         }
    66.         // If the entering collider is a map...
    67.         else if (other.gameObject.tag == "map" && Input.GetKey("e"))
    68.         {
    69.             itemweight = .2f;
    70.             ScoreManager.currentweight += itemweight;
    71.             other.gameObject.SetActive(false);
    72.             mapimage.color = visible;
    73.             isholding = true;
    74.             Color c = mapweightA.color;
    75.             c.a = 100;
    76.             mapweightA.color = c;
    77.         }
    78.         else
    79.         {
    80.         }
    81.     }
    82.  
    83.        
    84.     void DropItem()
    85.     {
    86.         if (bookimage.color == visible)
    87.         {
    88.             itemweight = 1.2f;
    89.             ScoreManager.currentweight -= itemweight;
    90.             bookimage.color = invisible;
    91.             Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
    92.             GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
    93.             item.gameObject.tag = "book";
    94.             item.gameObject.SetActive(true);
    95.         }
    96.         if (mapimage.color == visible)
    97.         {
    98.             itemweight = .2f;
    99.             ScoreManager.currentweight -= itemweight;
    100.             mapimage.color = invisible;
    101.             Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
    102.             GameObject item = Instantiate(map, position, Quaternion.identity) as GameObject;
    103.             item.gameObject.tag = "map";
    104.             item.gameObject.SetActive(true);
    105.         }
    106.         isholding = false;
    107.     }
    108. }
     
  2. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Have you looked at the book in the inspector? Note the scale before picking it up, the scale when you've picked it up, and again when you dropped it. Alternatively, you could make the book a prefab, destroy it upon picking it up, and instantiate it again when dropping it.
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,385
    You're instantiating a GameObject from a scene reference (bad). Make the book a prefab and instantiate it from the Project Hierarchy.
     
    Hotpots likes this.
  4. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    @Elmdran The scale is 1 in the scene and 1 in the inspector. Also I do have a book prefab, however am I right that in destroying an item you cannot instantiate it as it's destroyed? Therefore I used Item.setactive?

    @LaneFox Do you mean something like this?
    Code (CSharp):
    1. public GameObject bookt;
    bookt is the book prefab.
    Code (CSharp):
    1. /*GameObject item =*/ Instantiate(bookt, position, Quaternion.identity) /*as GameObject*/;
    Because it is not working for me, I mean it is instantiating the object however at the same small size. :(
     
  5. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Yes you need to retain the reference to the object if you want to instantiate it back again after destroying it, hence the use of prefab[/QUOTE]

    replace GameObject book with public GameObject book, remove
    1. book = GameObject.FindGameObjectWithTag("book");
      from your script and instead drag the prefab into the box you get in the inspector.

      Then use

      GameObject go;
      go = Instantiate(.......) as GameObject,
     
    Hotpots likes this.
  6. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    @Elmdran Yup I've done that, and it all works perfectly expect it still instantiates the book at half it's original size :(

    EDIT: Ok so... I so when I drag the book prefab into the scene it is in fact very small, much like the object I instantiate. However when I scale the prefab the object in the scene also scales. Any reason why?


    EDIT2: I fixed the problem, was very simple, I was just very silly :p Thanks for your help guys!

    I have another small question, how would I implement a rigid body (physics) to the items so they fall?
    Everytime I add a rigid body they fall through the floor of the level even though the floor has a collider component.
     
    Last edited: Oct 27, 2015
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,385
    The rigidbody must also have a collider.
     
  8. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    The book (with the rigid body) has a mesh collider and the floor has a box collider.
     
  9. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,385
    Does your collision matrix prohibit them from colliding?
     
  10. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    uhmmm collision matrix? What's that, first time hearing that term. Sorry :p


    The floor inspector (I've tried toggling a box/mesh collider):


    The book inspector:
     
    Last edited: Oct 27, 2015
  11. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    ^^,

    When your object has the trigger box checked, it doesn't collide with anything
     
  12. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Oh gosh ok,
    But I'm using an on trigger event to pick up and drop my game object? So how would I solve this?

    As you see here:
    Code (CSharp):
    1.     void OnTriggerStay(Collider other)
    2.     {
    3.         // If the entering collider is a book...
    4.         if (other.gameObject.tag == "book" && Input.GetKey("e"))
     
  13. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Have a child gameobject with a trigger collider on the book
     
  14. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey Elmdran,
    I've made an empty child game object and attached a mesh collider to it with the "istrigger" ticked.
    Now once I pick the item up the book doesn't get destroyed although it does pick up the empty game object.

    Book Game object and child GO:


    Child GO "collider" inspector view:


    Book GO "Book_Item_t" inspector view:
     
  15. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Code (CSharp):
    1. void OnTriggerStay(Collider other)
    2.     {
    3.         // If the entering collider is a book...
    4.         if (other.gameObject.tag == "book" && Input.GetKey("e"))
    5.         }
    6.             PickupItem();
    7.             Destroy(other.gameObject);
    8.         }
    9. }