Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by consolleparadise · Feb 13, 2020 at 11:12 AM · scenesweapongameobject.findreference-other-object

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");
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image consolleparadise · Feb 13, 2020 at 03:12 PM 0
Share

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

avatar image tormentoarmagedoom consolleparadise · Feb 13, 2020 at 04:53 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3rd person weapon equiping 1 Answer

Put all info from one game object to another one 0 Answers

Communicate between scenes with prefabs? 3 Answers

A scene over another 1 Answer

Help ! Save scene and can load that scene with a button 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges