my weapon dont get reference trough scene
hi all i have this problem. i have a shop that work with dontdestroyonload in every scene and i able to get coins trough the scene and return to the shop.. the problem is that i have the object that if i buy the gun it will setactive.. the problem is that the object dont get the reference in scene becouse the object rifle get destroyed in every scene.. how can i reference it trough the different scene? i tried the GameObject.Find but it dont work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameControlScript : MonoBehaviour {
public Text moneyText;
public static int moneyAmount;
int isRifleSold;
public static GameObject rifle;
void Start () {
moneyAmount = PlayerPrefs.GetInt ("MoneyAmount");
isRifleSold = PlayerPrefs.GetInt ("IsRifleSold");
rifle = GameObject.Find("BullPup");
if (isRifleSold == 1)
rifle.SetActive (true);
else
rifle.SetActive (false);
}
// Update is called once per frame
void Update () {
moneyText.text = "Money: " + moneyAmount.ToString() + "$";
}
public void gotoShop()
{
PlayerPrefs.SetInt ("MoneyAmount", moneyAmount);
SceneManager.LoadScene ("Shop");
}
}
Answer by tormentoarmagedoom · Feb 13, 2020 at 02:51 PM
Hello there.
Yes. As the objects (weapons) are destroyed when changing scenes, you need some kind of code in ther DontDestroyOnLoad script to Search, find, and assign again all the weapons.
A good way can be something like this. (If dont know how to do it, google it the spets you need):
First, all weapons in the game should have a Tag like "weapon" or each familiy of weapons have iots own tag "sword" "bow" ...
As you know all weapons have a specyfic tag, You need to run a function everytime a new scene ha been loaded. For this you have 2 options:
Or use this: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html?_ga=2.48681327.897632599.1581510450-127694322.1580903177 Or you have a script (outside DontDestroyOnload script) that is placed in all scenes, and using the Start() function (be sure is the last script to be executed, see https://docs.unity3d.com/Manual/class-MonoManager.html)
In this function that is executed once the level has been loaded, use this function https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html to find all the objects in the scene with the tag. Once you have them, you can assign in the specyfic variable you need for yout other scripts, modify the weapons with the "upgrades" that the weapons had in the last scene, as your DontDestroyOnload script should have registred that "upgrades" as a string or int or any other class variable.
Is this what u was looking for?
Byee!
Hi and thanks for the help. The thing i need Is simple. Is like public GameObject weapon, like this i can put the weapon in the slot and work the scripts i have. The problem Is that the dontdestroyonload destroy the Object when changing scene. I need the reference of the GameObject in every scene like It Is public GameObject weapon;.. all the weapons have tag in it.. i need that the script setactive true,false the GameObject
Hello. Yes. I understand, and i what i told you. AS all scenes will ned the reference to the new created objects, you need to find them and assign them via code, not from the editor.
You must find the object in the scene every tiem a new scene is loaded, and assign each weapon to its variables to continue. For do this, follow my post.
Your answer
Follow this Question
Related Questions
How can you do borderlands style random weapons and stat generation? 2 Answers
Pick up Weapons 1 Answer
Having a few script errors that I ran into. 1 Answer
Working with multiple scenes? 0 Answers
How to save volume when switching scene? 2 Answers