- Home /
Accessing gameobject from other scene
I have two scenes one is GUI and other is my main game scene i want to reference gameobject of my main game scene to my GUI scene on getting UI button. Basically i have three types of players and I want to choose one of them from my GUI.i made three UI botton and set my other two gameobjects inactive while one is active on UI input I am using findobjectwithtag but I don't know how to reference a gameobject from other scene
And sorry for my bad english ;_;
Answer by Magso · Jan 06, 2019 at 07:20 PM
Create 3 player prefabs and make a script that instantiates one of the player prefabs depending on which UI button you click. E.g. a script on each button to select the player
int playerNum;
void OnMouseDown (){
GameObject.Find("name").GetComponent<otherScript>().selected = playerNum;
SceneManager.LoadScene("Main Scene", LoadSceneMode.Single);
}
and script on a separate object to make the player
boolean playerCreated = false;
GameObject[] players;
int selected;
void Awake ()
{
DontDestroyOnLoad(gameObject);
}
void Update ()
{
if(!playerCreated && SceneManager.GetActiveScene().name == "Main Scene")
{
Instantiate(players[selected]);
playerCreated = true;
}
}
I'm not sure if there's a way to do it in a single script.
Thanks for the solution dude :)
It seems correct as per my requirement.
I will try it when I go home :)
:"( @Uni010-unity I have tried this code today But it returns an error which is Null reference exception:object reference not set to an instance of an object.
Which is showing error in the line
GameObject.Find("playermanager").GetComponent<playermanagerscript>(). Selected = playernum;
If the object is called "playermanager" it shouldn't have an error. You need both the scripts in the first scene where you pick the player, if you've put the latter script in the main scene then the first script will show an error because it's trying to find it.
Answer by SahanD · Jan 07, 2019 at 08:06 AM
you need to script for the scene01 and scene02. manuManager is the character selection script and GamePlay script is for the game.
Scene01
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MenuManager : MonoBehaviour {
//
// Your code
//
//above code is for the button and this will save your chatater identification,
pubic void Character01()
{
PlayerPrefs.SetInt("Character", 1);
}
}
GamePlay Script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GamePlay : MonoBehaviour {
static int CharacterMode = 0;
public GameObject player01;
public GameObject player02;
void Awake()
{
CharacterMode= PlayerPrefs.GetInt("Character"); //check character
}
void Update()
{
//Character Check
if(CharacterMode== 0)
{
Instantiate(player01);
}
if(CharacterMode== 1)
{
Instantiate(player02);
}
}
I'd recommend putting the players in an array, that way you don't have to keep adding if statements for every new player.
Instantiate(player[Character$$anonymous$$ode]);
Also checking the character in Update will create more of the same player every frame. Either use a boolean to stop it or put everything in Awake().
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Randomly choose children gameobjects in a for loop 1 Answer
How to make voxel map generator using 3D Perlin noise 0 Answers
Simple traffic for straight road 1 Answer