Hello everyone,
What I am trying to do is to move an object below another object but in a different scene.
Currently, I can only move that object when I click it into the other scene, thanks to the appropriate MoveGameObjectToScene() function, but what I can’t do is move it directly as a child of another object.
How can I do that?
You can use this to set the parent of a GameObject: Unity - Scripting API: Transform.SetParent
The scene that the other object lives in is not important, and setting the parent as an object from another scene will magically move your object into that object’s scene.
Where should I attach the script in an empty gameObject to the other scene? And a thing to know is that the object won’t always be the same, so I can’t attach the object to the script.
I’m sorry your questions are extremely vague. It’d help to explain exactly what you’re talking about. Are you talking about in the editor? At runtime in the game in a script? What?
Thank you for your patience.
I want to move the single object I select (I say single because I will instantiate more in scene 1) and move it under another game object in scene 2.
SCENE 1 —> Instantiate objects (A, B, C, D) in the screen —> Click on a single object (e.g., A) —> Object A in SCENE 2 under another gameObject.
I want to move object A (or B, C, D–depending on what the user chooses to click) as a child of the object in scene 2 via script.
I have already tried SetParent, pasting the script on an empty object in scene 2, but it doesn’t work.
What can I do to move the clicked object as a child of an object in scene 2?
SetParent would work just fine in this situation. You’d have to show the code you tried.
Okay, I’ll show you the code for Move and Select the object here (this is applied to a GameManager in the first scene):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ButtonAction : MonoBehaviour
{
public GameObject button;
private int index;
private void Start()
{
button = this.gameObject;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(LoadScene(index));
}
}
IEnumerator LoadScene(int index)
{
Scene currentScene = SceneManager.GetActiveScene();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit Hit;
if (Physics.Raycast(ray, out Hit, 100.0f) && Hit.collider.gameObject == gameObject.CompareTag("GP"))
{
index = 2;
button = this.gameObject;
DontDestroyOnLoad(button);
//Debug.Log("GroundScene");
//SceneManager.UnloadSceneAsync(currentScene);
}
else if (Physics.Raycast(ray, out Hit, 100.0f) && Hit.collider.gameObject == gameObject.CompareTag("MA"))
{
index = 1;
button = this.gameObject;
DontDestroyOnLoad(button);
//Debug.Log("GroundScene");
//SceneManager.UnloadSceneAsync(currentScene);
}
AsyncOperation scene = SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
//Wait until we are done loading the scene
while (!scene.isDone)
{
yield return null;
}
SceneManager.MoveGameObjectToScene(button, SceneManager.GetSceneByBuildIndex(index));
}
}
And this is the code attached in the second scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetChild : MonoBehaviour
{
ButtonAction buttonAction;
GameObject child;
public Transform parent;
private void Start()
{
child = buttonAction.button;
child.transform.SetParent(parent);
}
}