Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by dennz_liu · Jun 10, 2016 at 12:09 PM · gameobjectinstantiatetransformchildparent-child

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jun 10, 2016 at 03:14 PM 0
Share

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.)

1 Reply

· Add your reply
  • Sort: 
avatar image
17

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges