I’m trying to set the parent of one gameobject to a different gameobject and have no clue how to call it. I’ve looked online but I can’t find anything.
Any help would be much appreciated!
I’m trying to set the parent of one gameobject to a different gameobject and have no clue how to call it. I’ve looked online but I can’t find anything.
Any help would be much appreciated!
You have to have a reference to that gameobject. There are tons of examples of this. There are a bunch of examples on how to get this reference. This can include GameObject.FInd or a direct reference.
So, let’s just say I have a manager script
public class ManagerScript: MonoBehaviour
{
public GameObject player;
public GameObject playerBox;
public void MovePlayer()
{
player.transform.position = new Vector3(1f, 10f, 0f);
}
public void ChangePlayerParent()
{
player.transform.SetParent(playerBox);
}
}
In this case the manager would be in the scene and you would drag and drop the player object into the player variable, thus creating a reference. Then call MovePlayer to move the player or ChangePlayerParent to change it’s parent.
You can do this a few ways, but it’s a pretty common thing and you can find it in several tutorials.
There are a number of different ways. The most basic one is a public reference. Put this in your script:
public GameObject otherObject;
Now look at the object in the inspector. You should see a “slot” into which you can drag and drop other GameObjects.
Depending on where the other object is coming from, you can often get access to it via “something.gameObject”. For example, if it’s something that you just collided with, you can use collision.gameObject to get access to it.
If you can give us more context as to how you want the object to be “found”, we can give you better advice.
But DON’T use GameObject.Find. It’s one of those things that is very tempting because it’s simple and it works when you try it, but it’s a terrible, terrible habit. It’s slow, it’s unreliable (if anything at all happens to the name it will fail, or if the object later gets duplicated it could find the wrong one), and it makes your code very non-versatile. There is always - ALWAYS - another option that is better in every way than GameObject.Find for runtime code. The trouble is that there are half a dozen methods to learn how to use, so it’s tougher for newbies. But there’s always a better way, and the forum (me especially) will happily help you learn it.
Nothing you guys suggested has worked. Here is my code:
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
public class DropMinion1 : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
public GameObject minizone;
public void OnPointerEnter(PointerEventData eventData)
{
//Debug.Log("OnPointerEnter");
}
public void OnPointerExit(PointerEventData eventData)
{
//Debug.Log("OnPointerExit");
}
public void OnDrop(PointerEventData eventData)
{
Debug.Log(eventData.pointerDrag.name + " was dropped on " + gameObject.name);
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null)
{
d.parentToReturnTo = minizone.Transform;
}
}
// Update is called once per frame
void Update () {
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Transform parentToReturnTo = null;
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
parentToReturnTo = this.transform.parent;
this.transform.SetParent(this.transform.parent.parent);
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
//Debug.Log("OnDrag");
this.transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag");
this.transform.SetParent(parentToReturnTo);
GetComponent<CanvasGroup>().blocksRaycasts = true;
}
// Use this for initialization
void Start () {
}
I can tell you what we suggested isn’t wrong. But what doesn’t work? What part are you having issues with? I’m curious in what order OnDrop and OnEndDrag are executing.
Basically, I’m making a clone of Hearthstone where you drag cards out of your ‘hand’ and if you ‘drop’ them in a valid location then the card will be parented to the ‘minion zone’.
This issue I’m having is trying to parent the card to the ‘minion zone’ (which is a panel) when it is dropped a vaild area.
public void OnDrop(PointerEventData eventData)
{
Debug.Log(eventData.pointerDrag.name + " was dropped on " + gameObject.name);
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null)
{
d.parentToReturnTo = minizone.Transform;
}
}
^ This is the section of code I’m having trouble with ^
OK, so you’ve got your reference to the other object (d). When you say “parent the card…”, are you talking about setting its parent in the Unity object hierarchy? If so, that’d just be d.transform.parent = minizone.Transform. If not, then we need more information about what you mean by that.
What I can tell, OnDrop gets the Draggable reference from the card and sets a transform value on that card. Then the cards OnEndDrag is suppose to assign the parent using SetParent. I have not used OnDrop before myself. I guess the question is does OnDrop execute? Does it execute before OnEndDrag? Assigning the reference and the parent could both be done in OnDrop technically.
Awesome, worked it out.
public void OnDrop(PointerEventData eventData)
{
Debug.Log(eventData.pointerDrag.name + " was dropped on " + gameObject.name);
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null)
{
d.parentToReturnTo = minizone.transform;
}
}
Turns out that I was using a capital letter for ‘transform’ which Unity didn’t seem to like for some reason.
Transform is what you’d use when declaring a variable
transform is a reference to the transform attached to an object.
Good catch. Sometimes those get overlooked.