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

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):
```csharp
**using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ObjectPickup : MonoBehaviour
{
public Image bookimage;
public Image mapimage;

public Text bookweightA;
public Text bookweightS;

public Text mapweightA;
public Text mapweightS;

public static float itemweight;

bool isholding;
//float timeleft = 2;
Color visible = new Color(0f, 0f, 0f, 100f);
Color invisible = new Color(0f, 0f, 0f, 0f);

GameObject book;
GameObject map;

void Start()
{
    book = GameObject.FindGameObjectWithTag("book");
    map = GameObject.FindGameObjectWithTag("map");
    //anim = GetComponent<Animator>();
}


void Update()
{
    if (Input.GetKey("q") && isholding == true)
    {
        DropItem();
    }
}


void OnTriggerStay(Collider other)
{
    // If the entering collider is a book...
    if (other.gameObject.tag == "book" && Input.GetKey("e"))
    {
        itemweight = 1.2f;
        ScoreManager.currentweight += itemweight;
        other.gameObject.SetActive(false);
        bookimage.color = visible;
        isholding = true;
        Color c = bookweightA.color;
        c.a = 100;
        bookweightA.color = c;
        bookweightA.CrossFadeAlpha(0, 2, false);
        /*timeleft -= Time.deltaTime;
        Debug.Log(timeleft);
        if (timeleft <= 0)
        {
            c.a = 0;
            bookweightA.color = c;
            timeleft = 0;
        }*/
    }
    // If the entering collider is a map...
    else if (other.gameObject.tag == "map" && Input.GetKey("e"))
    {
        itemweight = .2f;
        ScoreManager.currentweight += itemweight;
        other.gameObject.SetActive(false);
        mapimage.color = visible;
        isholding = true;
        Color c = mapweightA.color;
        c.a = 100;
        mapweightA.color = c;
    }
    else
    {
    }
}

   
void DropItem()
{
    if (bookimage.color == visible)
    {
        itemweight = 1.2f;
        ScoreManager.currentweight -= itemweight;
        bookimage.color = invisible;
        Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
        GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
        item.gameObject.tag = "book";
        item.gameObject.SetActive(true);
    }
    if (mapimage.color == visible)
    {
        itemweight = .2f;
        ScoreManager.currentweight -= itemweight;
        mapimage.color = invisible;
        Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
        GameObject item = Instantiate(map, position, Quaternion.identity) as GameObject;
        item.gameObject.tag = "map";
        item.gameObject.SetActive(true);
    }
    isholding = false;
}

}**
```

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.

You’re instantiating a GameObject from a scene reference (bad). Make the book a prefab and instantiate it from the Project Hierarchy.

1 Like

@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?

public GameObject bookt;

bookt is the book prefab.

/*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. :frowning:

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

  • 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,

1 Like

@Elmdran Yup I’ve done that, and it all works perfectly expect it still instantiates the book at half it’s original size :frowning:

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

The rigidbody must also have a collider.

The book (with the rigid body) has a mesh collider and the floor has a box collider.

Does your collision matrix prohibit them from colliding?

uhmmm collision matrix? What’s that, first time hearing that term. Sorry :stuck_out_tongue:

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

The book inspector:

^^,

When your object has the trigger box checked, it doesn’t collide with anything

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:

    void OnTriggerStay(Collider other)
    {
        // If the entering collider is a book...
        if (other.gameObject.tag == "book" && Input.GetKey("e"))

Have a child gameobject with a trigger collider on the book

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:

void OnTriggerStay(Collider other)
    {
        // If the entering collider is a book...
        if (other.gameObject.tag == "book" && Input.GetKey("e"))
        }
            PickupItem();
            Destroy(other.gameObject);
        }
}