Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false.
Hi everyone,
I try to create an inventory follow the tutorial from https://www.youtube.com/watch?v=6twPV3e4crE
But when i try to copy the game object named Slot using instantiate, I got an error exclamation like this :
Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues. UnityEngine.Transform:set_parent(Transform) inventory:addChilParent(GameObject, GameObject) (at Assets/script/inventory.cs:54) inventory:Start() (at Assets/script/inventory.cs:27)
I dont know what happen to this script. But it seem all is Ok. I do exactly same as the the tutorial.
Below is my code :
item.cs
using UnityEngine;
using System.Collections;
// Make Class Item
public class item {
public string itemName;
public int itemID;
public string itemDesc;
public Sprite itemIcon;
public GameObject itemModel;
public int itemTime;
public int itemValue;
public int itemStock;
public ItemType itemType;
private string baseName;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void Awake () {
}
public enum ItemType {
AnimalBarm,
Mining,
Corps,
Dairy,
JuiceJamMaker,
AnimalFood,
Kitchen,
Bakery,
CraftHouse,
RAM
}
public item (string name, int ID, string desc, int time, int value, int stock, ItemType type, string folder) {
itemName = name;
itemID = ID;
itemDesc = desc;
itemTime = time;
itemValue = value;
itemStock = stock;
itemType = type;
this.baseName = folder + "/";
itemIcon = Resources.Load<Sprite> (this.baseName + itemName);
}
public item() {
}
}
itemDatabase.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class itemDatabase : MonoBehaviour {
public List<item> items = new List<item>();
// Use this for initialization
void Start () {
// Add Data To Item List With Class Item
items.Add (new item ("Apple", 0, "Red Sweet Apple", 720, 25, 5, item.ItemType.Corps,"Corps"));
items.Add (new item ("BlueBerry", 1, "Sweet BlueBerry", 1440, 88, 5, item.ItemType.Corps,"Corps"));
items.Add (new item ("Cabbage", 2, "This is my Cabbage", 3, 12, 5, item.ItemType.Corps,"Corps"));
items.Add (new item ("Corn", 3, "Yellow Corn Sweet", 5, 13, 5, item.ItemType.Corps,"Corps"));
items.Add (new item ("Lemon", 4, "Honey Lemon", 1020, 75, 5, item.ItemType.Corps,"Corps"));
items.Add (new item ("Wheat", 5, "Natural Wheat", 2, 10, 5, item.ItemType.Corps,"Corps"));
}
// Update is called once per frame
void Update () {
}
}
Inventory.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class inventory : MonoBehaviour {
public List<GameObject> slotsx = new List<GameObject> ();
public List<item> itemx = new List<item> ();
public GameObject slots;
public GameObject toolTip;
public GameObject dragitemicon;
public bool draggingitem = false;
itemDatabase database;
// Use this for initialization
void Start () {
int slotAmount = 0;
database = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<itemDatabase> ();
// Generate the Slot and Slot Name;
for(int i = 1; i <= 24; i++) {
GameObject Slot = (GameObject) Instantiate(slots);
//Slot.GetComponent<slotScript>().slotNumber = slotAmount;
slotsx.Add(Slot);
itemx.Add(new item());
addChilParent (this.gameObject,Slot);
//Slot.transform.parent = this.gameObject.transform;
Slot.name = "slot-" + i;
slotAmount++;
}
AddItem (0);
AddItem (1);
AddItem (2);
AddItem (3);
AddItem (4);
AddItem (5);
}
// Update is called once per frame
void Update () {
if (draggingitem) {
Vector3 post = (Input.mousePosition - GameObject.FindGameObjectWithTag("Canvas").GetComponent<RectTransform>().localPosition);
//Vector3 post = new Vector3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z);
//Debug.Log (post);
//Vector3 post = Input.mousePosition;
dragitemicon.GetComponent<RectTransform>().localPosition = post;
}
}
public void addChilParent(GameObject parentx, GameObject childx) {
childx.transform.parent = parentx.gameObject.transform;
}
void AddItem(int ID) {
for (int i = 0; i < database.items.Count; i++) {
if(database.items[i].itemID == ID) {
item itemxs = database.items[i];
AddItemEmptySlot(itemxs);
break;
}
}
}
void AddItemEmptySlot (item items) {
for (int i = 0; i < itemx.Count; i++) {
if(itemx[i].itemName == null) {
itemx[i] = items;
break;
}
}
}
public void showToolTip ( Vector3 position, item item) {
toolTip.SetActive (true);
toolTip.GetComponent<RectTransform> ().localPosition = new Vector3 (position.x + 70, position.y - 50, position.z);
toolTip.transform.GetChild (0).GetComponent<Text> ().text = item.itemName;
toolTip.transform.GetChild (1).GetComponent<Text> ().text = item.itemDesc;
toolTip.transform.GetChild (2).GetComponent<Text> ().text = item.itemTime.ToString();
toolTip.transform.GetChild (3).GetComponent<Text> ().text = item.itemValue.ToString();
toolTip.transform.GetChild (4).GetComponent<Text> ().text = item.itemStock.ToString();
}
public void closeToolTips () {
toolTip.SetActive (false);
}
public void showDragItem (item item) {
dragitemicon.SetActive (true);
draggingitem = true;
dragitemicon.GetComponent<Image> ().sprite = item.itemIcon;
}
}
slotScript.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class slotScript : MonoBehaviour, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler, IDragHandler {
public item itemx;
Image itemImage;
public int slotNumber;
inventory inventoryx;
// Use this for initialization
void Start () {
inventoryx = GameObject.FindGameObjectWithTag ("inventory").GetComponent<inventory> ();
itemImage = gameObject.transform.GetChild (0).GetComponent<Image> ();
}
// Update is called once per frame
void Update () {
if (inventoryx.itemx[slotNumber].itemName != null) {
itemx = inventoryx.itemx[slotNumber];
itemImage.enabled = true;
itemImage.sprite = inventoryx.itemx[slotNumber].itemIcon;
} else {
itemImage.enabled = false;
}
}
public void OnPointerDown (PointerEventData data) {
Debug.Log (transform.name);
}
public void OnPointerEnter (PointerEventData data) {
if (inventoryx.itemx [slotNumber].itemName != null) {
inventoryx.showToolTip(inventoryx.slotsx[slotNumber].GetComponent<RectTransform>().localPosition,inventoryx.itemx[slotNumber]);
Debug.Log(inventoryx.slotsx[slotNumber].GetComponent<RectTransform>().localPosition);
}
}
public void OnPointerExit (PointerEventData data) {
if (inventoryx.itemx [slotNumber].itemName != null) {
inventoryx.closeToolTips ();
}
}
public void OnDrag (PointerEventData Data) {
if (inventoryx.itemx [slotNumber].itemName != null) {
inventoryx.showDragItem(inventoryx.itemx[slotNumber]);
}
}
}
The error exclamation is on script inventory.cs which is on line : addChilParent (this.gameObject,Slot); and
on method : childx.transform.parent = parentx.gameObject.transform;
The gameobject was copied and run successfull same as the tutorial but mine got error exclamation. Thats all the code i type. The video i have followed is from part 1 to part 4. and the problem is exist on part 1.
What is the meaning of the error exclamation and how to fix it ?
Thanks Everyone.
They're probably using an older version of Unity. You could ask them if they could update it. Or maybe this never worked in the first place (anyone can post on u-tube.) Or you could look up the error and fix it yourself (lots of people ask the same Q here.)
But it's probably using an out-dated UI system, so best just to find a newer example (if you think you'll ever want to modify it.)
Answer by dennz_liu · Jun 10, 2016 at 03:44 PM
Hi Owen Reynolds,
I just have finished the problem. Maybe it is and older version of unity so the code : childx.transform.parent = parentx.gameObject.transform; is not work anymore in new version.
But i have change set parent to this code : childx.transform.SetParent (parentx.gameObject.transform); and it work like a charm.
So the problem here is just older version VS new version syntax of using the function is differrent.
Thanks
Your answer
Follow this Question
Related Questions
How to find the transform position of another gameobject then move a gameobject to that position? 1 Answer
Child Instantiated GameObject to GameObject hit by raycast 1 Answer
Instatiate a gameobject/prefab on the parent's current location if the child is destroyed. 1 Answer
Canon Ball shooting with Instiate 1 Answer
Parenting Instancing and Prefab issues 0 Answers