accessing a gameobject in different scene

within a class there are reference to gameobjects in the scene A,

the same gameobjects are available in the scene B.

i have dragged the gameobjects in scene A into inspector fields. now how to reference these gameobjects in the scene B via script into relevant fields in the inspector?

please give me some idea. thanls.

(SOLVED) Data Access from another script
Why isn't my variable carried over?
Navigation UI System with Camera move in 3D with Panels
Acceed variables from other scripts
Calling function from other scripts c#
Update ammo with {get; set}
Pass a variable from one object to another.
Click on an object and have it trigger code on another game object...
Get variable from another script
NullReferenceExp. I really Need Help!
Object Reference not set to an instance of an object
Need help getting a 3d object to destroy when hit by a 2d bullet in a 2d game
Not sure how to call a public void from another script
How to acces and change the float that is in diffrent script?
Anyone have tips on making an item shake after a wrong guess?
keyword public not working
get and set property from other game object script....
How to properly call a variable in a separate script
Uses same variable in different scripts
How to link one script function to another script
How do you transfer variable values between scripts
How to check bools in a different scirpt
Reading variables from another script?
How Do I Write a Script to Change the Audio Clip on an Audio Source?
how to change make my movement (rigidbody) relative to my camera? (cinemachine)
How To Get A Field,Type,Value of a variable on another script
Help referencing a script from another game object
Rotate an object from another script
trying to run the class from one script in another script I get an error Please Help me?
Help, how do i call variables from another script
how do i reference another gameobject's variables correctly?
Check if a script of another object is on? (C#)
How do I make it so that when my snake picks up a food it gets +1 point added?
Help im a new coder and i dont know how to tie variables
how do i made a script interact with another?
Access/enable/disable bool from another script
How to change/set animator controller from other script?
Is this a proper way for one script to reference another?
GetType.GetField.GetValue from another script as GameObject
How do I change vaule of one script using another?
Passing an object as an argument
Help needed accessing a variable from another script
man I really dont understand "talking" between scripts
Which function to acces another scripts
how to refrence object
Need help with variable referencing in another script
Which Function I Need to Use to Receive Figures from Another Script?
I am having trouble getting a reference to another class
Cannot Set a variable of a class within another clas
Can't access one class from another
How do I link scripts?
Referencing a Method in another Script - Beginner
How to sent the transform value of a Gameobject to another script?
accessing bool from another class not working
How to reference a variable from another script without using static variables
Trouble disabling a script from another script
Can't get variable from another script
How to you see if a bool is true for false on a different object
Trying to access childrens variables from parent
Transform.position to an emty game object
Trying to access a function in one script from another
Calling Rigidbody from another script and appying AddForce to it.
call function in clone
How to connect two different scrip?
I need help accessing my float between 2 scripts
How can I pass on the variable from another script to another?
How can I call a function from a nearby gameobject's script?
Best way to get variables/components from other scripts
Stop moving whilst attacking
Is there a way to get a value of a variable from a script to onother?
How do I access the Animator component in this case?
How to get a bool from another class that is get/set
bool only equling at true in one script but not the other
Need help refracing script inside text
Hi! how do i access a randomly generated string in one script from another script?
How do I automate getting the getting GameObject into a script?
NullReference exception() even when i have everything linked in the hierarchy
How to make a reference to a script?
Getting instance of an object from another objects script
How to create a homing missile in reference to another script
Destroying Clones Simple Issue
Value not subtracting and I don't know why
Trying to pass variables from one script to another
Calling a Scriptable Object using a String
How to reference a public variable from a script in one scene, in a script from another scene
refrencing a script within a script
How to access a Game Object from another script in Unity?
Need help acessing variable from different script.
Is it possible to change the parent of an instantiated prefab from another script?
Referencing instantiated scripts
how do i change a bool variable from true to false from a different script than the variable is in?
how to make my enemy attack the player?
Pls Help Me Reference a Function from Another Script
Get a float from another Object/script
IF/THEN Ranged Statements + Other script Interaction
Get Vector3 way points from another script.

When you use multiple scenes you need to dynamically locate the other resources in the other scene via some mechanism.

Your code must also be tolerant of other objects not being immediately ready, as all scene loading completes at the end of the current frame.

Locator patterns that can work to notify consumers of later-added resources include:

  • singleton

  • FindObjectOfType()

  • static mailbox

  • other less-reliable forms of Find() type mechanisms (name, tag, etc.)

etc.

The actual process is remarkably simple, but of course the devil is in the details and the timing.

As with ALL new technology and techniques, DO NOT integrate this into your actual game first.

Instead start a fresh small scene with a test locator script on it, and load a second scene that contains the desired object.

Work with only those two parts and PLENTY of Debug.Log() statements scattered everywhere until you fully understand what is involved.

4 Likes

Unless you mean that you loaded scene A first and set the objects to DontDestroyOnLoad (in which case you already have references to them), then the objects in scene B are not “the same” objects. They might be similar objects, but that doesn’t help you get a reference to them (at least not directly).

The typical way to obtain a reference at runtime is to use one of the “Find” functions, like GameObject.Find, Transform.Find, FindObjectsOfType, etc.

You can also copy a reference from some other variable, but if the variable is just located on another GameObject then you still need a reference to that object. You can possibly get around this using static variables.

1 Like

i did as you said and also the other friend above said. but the problem is in the second scene , because the game objects are diable , singleton dont find them . and when i click and game objects are displayed, referencing do not occur.

what i do to fix it?

here is code:

public class AudioManager : MonoBehaviour
{
    private static AudioManager instance = null;

    public static AudioManager Instance
    {
        get { return instance; }
    }

    [SerializeField]
    private AudioMixer audioMixer;

    public Image musicOnIcon;
    public Image musicOffIcon;

    public Image sfxOnIcon;
    public Image sfxOffIcon;

    private bool _musicIsMuted = false;
    private bool _sfxIsMuted = false;

    private const string MUSIC_PREFS_KEY = "musicmuted";
    private const string SFX_PREFS_KEY = "sfxmuted";

    private const string AUDIO_MIXER_MUSIC = "music";
    private const string AUDIO_MIXER_SFX = "sfx";

    private const int MUTED = 1;
    private const int NOT_MUTED = 0;

    private const float MUTED_VALUE = -80f;
    private const float NOT_MUTED_VALUE = 0f;



    private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(this.gameObject);
    }

    private void Start()
    {
        musicOnIcon = GameObject.Find("on_musics").GetComponent<Image>();
        musicOffIcon = GameObject.Find("off_musics").GetComponent<Image>();

        sfxOnIcon = GameObject.Find("on_sfx").GetComponent<Image>();
        sfxOffIcon = GameObject.Find("off_sfx").GetComponent<Image>();

Back up as I noted above and start with two simple scenes.

You problem indicates that you have not understood the lifetime scope and timing of things appearing, disappearing, lingering, don’t destroy, and generally how unrelated unconnected items in a Unity game successfully locate each other.

You have GOT to understand this fully before you even begin to attempt it within your actual game.

To do otherwise is just going to waste your time, confuse you with the extra complexity of your game, and possibly even damage other parts of your codebase as you experiment.

You must get practical hands-on experience with the multi-scene timing or you will not be able to reason about your issue.

ALSO: anyone reasoning about your script here in a vacuum probably cannot help, as it is intimately tied to timing and the particular way you load the scenes: additively? sequentially? some with a singleton? something else? The possibilities are limitless and you need to be able to reason about them.

3 Likes

i made in scene B the parent object active , but again also , it is not found . why?

so how is possible that singleton can access a deactive gameobject in another scene? this gameobject is a child inside ui canvas by the way.

i need a beginner help please.

What sort of information do you need from the other object? Is it just a variable/Value? Perhaps a Scriptable Object would be more straight forward.

2 Likes

it is like this:

you bring your gameobject from scene a to scene b.

in scene b , you have ui image which is deactive . but in your gameobject you have fields which need to be accessing that deactive ui image, how will you access that ui image?

hope it is clear to you.

by the way the gameobject which comes to scene b is a singleton.

any idea please?

You can’t just keep a UI Image. They need a canvas and all the other stuff they come with above them.

My first reply offers three completely-reliable approaches to finding or tracking stuff. If you put it in the scene in the first place, keep a reference to it somewhere, such as with a GameManager. There thousands of GameManager tutorials on Youtube. Start there and make sure you take the time for Step #2 below:

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

3 Likes