- Home /
Load character from character selector to the game scene
Hi everyone!
I'm developing a mobile game: It's a game where you have a cannon on wheels and you have to shoot the boxes to get points.
Here there is a menu to choose his cannon. When you press GO !, the chosen cannon should appear on the gamescene (the cannons are already on the gamescene but are disabled). But unfortunately it does not work.
Here are the scripts I did for the guns to load :
using System.Collections;
using UnityEngine;
public class DataHandler : MonoBehaviour
{
// Start is called before the first frame update
public int cartSel;
void Awake()
{
GameObject.DontDestroyOnLoad(this.gameObject);
}
void Start()
{
cartSel = 1;
}
public void selectCart(int a)
{
cartSel = a;
}
}
and this one
using UnityEngine;
using System.Collections;
public class DataImplementer : MonoBehaviour {
int cartSelected;
GameObject Default;
GameObject Gunner;
GameObject Titan;
GameObject Berserk;
GameObject Schockwave;
GameObject Undertaker;
GameObject Sparky;
void Start(){
cartSelected = GameObject.Find("DataHandler").GetComponent<DataHandler>().cartSel;
if (cartSelected == 1) {
Default.SetActive(true);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 2) {
Default.SetActive(false);
Gunner.SetActive(true);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 3) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(true);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 4) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(true);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 5) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(true);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 6) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(true);
Sparky.SetActive(false);
}
else if (cartSelected == 7) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(true);
}
}
}
I hope someone could fix my problem
Thanks :) (sry for my bad english)
You need to look into prefabs and instantiation. Also some arrays, lists, and general organization could do you some good. Not trying to sound rude, but what you are attempting is incredibly simple for program$$anonymous$$g in Unity. So you just need to do some more learning. I'll point you in the right direction though. Create your turret prefabs, give them an ID or something, and either load them from resources or put the references to the turrets in both scenes. When the player selects the turret, set a field in the player data class (if you made one) or in some static field area for the ID of the turret. When you start your play scene, get the ID of the turret you selected in main screen and instantiate that from your list of turret references at the desired spawn point. Does this make sense?
Answer by unity_J016ZU_VZQzYAg · Mar 31, 2019 at 07:24 PM
Could it be, that the Start Method got already called, so nothing is actually happening when you select a canon?
Add a method to the DataImplementer Script like showSelectedCanon(int sel)
and put the body of the start method as body of this method.
Like that:
void showSelectedCanon(int sel){
cartSelected = sel;
if (cartSelected == 1) {
Default.SetActive(true);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 2) {
Default.SetActive(false);
Gunner.SetActive(true);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 3) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(true);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 4) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(true);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 5) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(true);
Undertaker.SetActive(false);
Sparky.SetActive(false);
}
else if (cartSelected == 6) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(true);
Sparky.SetActive(false);
}
else if (cartSelected == 7) {
Default.SetActive(false);
Gunner.SetActive(false);
Titan.SetActive(false);
Berserk.SetActive(false);
Schockwave.SetActive(false);
Undertaker.SetActive(false);
Sparky.SetActive(true);
}
}
Now if you call that method after selecting a canon, it should work. Like:
public void selectCart(int a)
{
cartSel = a;
// call showSelectedCanon here
}
Your answer
Follow this Question
Related Questions
Save & Load Game question 3 Answers
Android load/unload times 0 Answers
Problem with saving the transform of the player 1 Answer
How to have character custimation to save and be used in an mmo 2 Answers