Okay. I have 2 prefabs for characters. In main menu you have models of them which can be selected which one of these you want by deactivating the other one. I made this
void Awake()
{
DontDestroyOnLoad(Character);
DontDestroyOnLoad(GCharacter);
}
I have button that load other scene and by this code I detect which prefab I want to spawn but don’t working
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System;
public class Spawner : MonoBehaviour
{
public GameObject CharacterMainMen;
public GameObject GCharacterMainMen;
public Transform Spawnpoint;
public Transform SpawnpointG;
public GameObject CharacterPrefab;
public GameObject GCharacterPrefab;
void Start ()
{
CharacterMainMen = GameObject.FindWithTag("CharacterMainMen");
GCharacterMainMen = GameObject.FindWithTag("GCharacterMainMen");
if (CharacterMainMen.activeSelf == true)
{
SpawnB();
}
if (GCharacterMainMen.activeSelf == true)
{
SpawnG();
}
}
void Update ()
{
}
void SpawnB()
{
Instantiate(CharacterPrefab, Spawnpoint.position, Spawnpoint.rotation);
}
void SpawnG()
{
Instantiate(GCharacterPrefab, SpawnpointG.position, SpawnpointG.rotation);
}
}
It works only if I selected The Character Model in the main menu and in unity says this error:
NullReferenceException: Object reference not set to an instance of an object Spawner.Start () (at Assets/Scenes/Spawner.cs:27)
NullReferenceException: Object reference not set to an instance of an object Spawner.Start () (at Assets/Scenes/Spawner.cs:22)