If you are using the new ui elements, everything is rendered in the order they are listed. So
Parent
-child (rendered first)
-child (rendered next)
-child (rendered last)
So whatever is at the bottom is always drawn on top of the others.
If you need to move stuff around, there are a few ways to do it.
First, a canvas does have the option to sort them. But, if you’re just putting stuff under one canvas, your best bet is just to move the order of the children around.
You can use setaslastsibling
So if you want the first child in the list to display on top of the others, you set it as the last sibling and it will now display in front of everything else in that canvas.
I have this script (which would seem to be the best of my efforts) attached to the child Panel and it is not working
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class SetCardLast : MonoBehaviour {
private RectTransform MoveToFront;
void Awake ()
{
MoveToFront = GetComponent <RectTransform> ();
MoveToFront.SetAsLastSibling ();
}
}
Is your intent to trigger it in awake? I’m not sure how your game is laid out, but if you have this on a “card” object and you have several cards in the game, each one tries to move to front right away and only that one time, after that, you have nothing to trigger it. If you follow the example in the link, it shows you one where they are clicking on the object which will bring it to the front.
You need to figure out how you want to trigger the code and set it up for that.
So whenever you make the card “rise up” you’ll also need to set it as last sibling at that point. Right now, you’re not doing anything to make it come forward except at the start of the game.
using UnityEngine;
using System.Collections;
using InControl;
public class ShowCard : MonoBehaviour {
private GameObject hand;
// Use this for initialization
void Start () {
hand = GameObject.FindGameObjectWithTag ("ChoiceCard");
}
// Update is called once per frame
void Update () {
if (InputManager.ActiveDevice.DPadRight.WasPressed)
{
hand.gameObject.transform.SetAsLastSibling ();
}
if (InputManager.ActiveDevice.DPadLeft.WasPressed)
{
hand.gameObject.transform.SetAsFirstSibling ();
}
}
}
The error is : MissingReferenceException: The object of type ‘Object’ has been destroyed but you are still trying to access it.
Which is very frustrating as I get that same error trying to Instantiate the “card” PreFab.
I had just been working on the cards in scene but decided to move on getting the cards to Instantiate until I could figure out this SetLast thing.
So now same error with getting cards into scene:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DrawCard : MonoBehaviour {
GameObject card = Resources.Load<GameObject>("Assets/Prefabs/HandCard");
public Transform Deck;
// Use this for initialization
void Start () {
GameObject newCard = Instantiate (card, Deck.position, Deck.rotation) as GameObject;
newCard.name = gameObject.name;
newCard.transform.parent = gameObject.transform;
}
// Update is called once per frame
void Update () {
}
}
I have not coded in a long time and this is just not clicking for me.
One suggestion is you make your GameObject card public and instead of trying to set it = there (which I think doing that actually doesn’t work the way you have it) is to just drop the prefab into the slot in the inspector.
Unless you’re using an older version of unity, should use setparent instead
Make sure Deck is set to something in the inspector.
In your first script, my guess is hand is targeting the wrong thing, or you’re just not updating it’s value. So when it tries to set it as last, it no longer exist.
I know what you’re trying to do, I just don’t understand how you’re trying to do it.
You may need to throw in some print or debug.log statements to make sure your variables are targeting what you think they are suppose to be.