- Home /
Duplicate UI object
In my game, I have a UI shop, where the player can purchase weapons and gear. The goal is to be able to drag goods from the store slots into the player's inventory slots, and have the items in the store duplicate upon being placed in the players inventory to allow the player to purchase a number of a particular good limited only by the player's cash and not the amount held by the store. Currently, I have just a panel inside(and child of) a panel and I am trying to duplicate the inner panel and set the parent of the duplicate equal to the parent of the inner panel, but, even this, I can't seem to get right.
After entering play mode I get this error: NullReferenceException: Object reference not set to an instance of an object StoreCopy.Start () (at Assets/StoreCopy.cs:16) Line 16 : Store = box.transform.parent.parent.parent.parent.gameObject;
MY CODE:
public class StoreCopy : MonoBehaviour {
GameObject Store;
GameObject boxClone;
void Start() {
GameObject box = this.gameObject; // sets box to GameObject this script is attached to
Store = box.transform.parent.parent.parent.parent.gameObject; // set store equal to parent of box
boxClone = Instantiate (box) as GameObject; // clone box
boxClone.transform.SetParent(Store.transform);
}
}
Answer by Fork_Spoon_Knife · Apr 12, 2018 at 07:05 PM
After some time spent in the Unity API, I found an alternative to using SetParent ( ) and the concomitant transform.parent mischief by using UnityEditor.GameObjectUtility.SetParentAndAlign (GameObject, Parent); and assigning Parent in the Inspector.
Answer by Cynikal · Apr 01, 2018 at 09:46 PM
Check to make sure Store is actually being assigned.
If (Store) boxClone.transform.SetParent(Store.transform); else Debug.Log("No store detected.");
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
instantiated object not moving with parent transform. 0 Answers
Multiple Cars not working 1 Answer
Trying to Instantiate() an object without cloning the code 2 Answers