How to get character from another scene? Character selection game
Hi. I really need help. I'm still new in making game. so i wanted to make a character selection. but I cannot get it into another scene. because the character that I wanted is a gameobject.
this is the script on the selection scene
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
public class selector : MonoBehaviour
{
public GameObject Player;
public GameObject Player2;
private Vector3 CharacterPosition;
private Vector3 OffScreen;
private int CharacterInt = 1;
private SpriteRenderer PlayerRender, Player2Render;
private readonly string selectedCharacter = "SelectedCharacter";
private void Awake()
{
CharacterPosition = Player.transform.position;
OffScreen = Player2.transform.position;
PlayerRender = Player.GetComponent<SpriteRenderer>();
Player2Render = Player.GetComponent<SpriteRenderer>();
}
public void nextbutton()
{
switch(CharacterInt)
{
case 1:
PlayerPrefs.SetInt(selectedCharacter, 1);
PlayerRender.enabled = false;
Player.transform.position = OffScreen;
Player2.transform.position = CharacterPosition;
Player2Render.enabled = true;
CharacterInt++;
break;
case 2:
PlayerPrefs.SetInt(selectedCharacter, 2);
Player2Render.enabled = false;
Player2.transform.position = OffScreen;
Player.transform.position = CharacterPosition;
PlayerRender.enabled = true;
CharacterInt++;
reset();
break;
default:
reset();
break;
}
}
public void previousbutton()
{
switch (CharacterInt)
{
case 1:
PlayerPrefs.SetInt(selectedCharacter, 2);
PlayerRender.enabled = false;
Player.transform.position = OffScreen;
Player2.transform.position = CharacterPosition;
Player2Render.enabled = true;
CharacterInt--;
reset();
break;
case 2:
PlayerPrefs.SetInt(selectedCharacter, 1);
Player2Render.enabled = false;
Player2.transform.position = OffScreen;
Player.transform.position = CharacterPosition;
PlayerRender.enabled = true;
CharacterInt--;
break;
default:
reset();
break;
}
}
private void reset()
{
if(CharacterInt >= 2)
{
CharacterInt = 1;
}
else
{
CharacterInt = 2;
}
}
public void ChangeScene()
{
SceneManager.LoadScene(1);
}
}
this is the script on the game scene
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
public class GetCharacter : MonoBehaviour
{
public GameObject player, player2;
private SpriteRenderer play, play2;
private readonly string selectedCharacter = "SelectedCharacter";
void Awake()
{
play = this.GetComponent<SpriteRenderer>();
}
void Start()
{
int getcar;
getcar = PlayerPrefs.GetInt(selectedCharacter);
switch (getcar)
{
case 1:
play.gameObject = player;
break;
case 2:
play.gameObject = player2;
break;
default:
play.gameObject = player; ;
break;
}
}
}
please help.
Comment