- Home /
Question by
Kingaroon · Jan 10, 2018 at 06:30 PM ·
c#gameobjectinstantiategame
Object instantiating in a different transform when parent is active
So I'm working on a system in my game where I can walk over an object and it appears in the inventory. The issue I'm having is that when the inventory parent is set active, the object transform does not align with the slot, but when the parent is not active, the object position is zero. This is my code instantiating the object:
if (items [i].ID == -1) {
items [i] = itemToAdd;
GameObject itemObject = Instantiate (inventoryItem);
itemObject.GetComponent<ItemData> ().item = itemToAdd;
itemObject.GetComponent<ItemData> ().amount = 1;
itemObject.GetComponent<ItemData> ().slot = i;
itemObject.transform.SetParent (slots [i].transform);
itemObject.transform.position = Vector2.zero;
itemObject.GetComponent<Image> ().sprite = itemToAdd.Sprite;
itemObject.name = itemToAdd.Title;
break;
}
Any help would be appreciated.
Comment
Your answer
Follow this Question
Related Questions
How to not instantiate an object if there is another object there? 1 Answer
Instantiated Prefab doesn't find main camera 2 Answers
How can I check if an instantiated object collides with another instantiated object? 1 Answer
Piece of code creates unwanted GameObjects in scene 3 Answers
C# Why do I have to use .AddComponent and not "new (ClassName)"? 1 Answer