- Home /
how to call function from object that is DontDestroyOnLoad ?
I tried FindGameObjectWithTag
but it says "cant converter void to gameobject"
because obviously there is no gameobject with that tag in that scene, its from another scene.
and I have no idea how to call a function if gameobject is in another scene.
so please help. thanks.
Answer by misher · Sep 04, 2018 at 01:02 PM
If you want to access the script attached to other gameobject in the same scene, you can use this:
var myScript = FindObjectOfType<MyScript>();
myScript.MyFunction();
It is a bit slow, so do not do it every frame (like inside Update() ). Put your "MyScript" gameobject on top of scene hierarchy would be better.
thanks for the reply but gameobject is in another scene.
You can't access object on another scene, they just don't exist. If your object is preserved on gameobject with DontDestroyOnLoad and therefore exists in the new scene where you want to call it, then all should work just fine.
Except if the scene is loaded additively, or the object has been moved to the "DontDestroyOnLoad" scene (since Unity 5) because of calling DontDestroyOnLoad()
on it, then they do exist.
Answer by myzzie · Sep 04, 2018 at 05:40 AM
The problem is not DontDestroyOnLoad, the problems is that you're not calling the gameobject the script is attached to.
GameObject.FindGameObjectWithTag("Tag").GetComponent<ScriptContainsDontDestroyOnLoad>().FunctionYouWantToCall();
A gameobject with dontdestroyonload is in your new scene on awake, so calling this from awake will not guarantee to find it.
yes I did the same thing
private GameObject myObject;
myObject= GameObject.FindGameObjectWithTag("myTag").GetComponent<myScript>().myFunction();
but I'm trying to call it from another scene where gameobject with myTag doesnt exist.
thats why it says cant convert void to gameobject.
It doesn't matter if it's in a different scene (if the scene is loaded), FindGameObjectWithTag()
should find it (if it has that tag).
Please post more of the relevant parts of your script, it is difficult to help if only certain lines are shown, without context.
hi, thanks for the reply, sorry if I was not being clear enough.
let me explain again what I'm trying to do.
there is 1st scene, that has main menu and stuff. it also has empty gameobject called ads with scripts for admob.
there is another scene, that has a button and script for that button.
when user presses that button, ad is shown, because currently I have ads gameobject in evey scene.
but I was thinking can I add DontDestroyOnLoad to ads gameobject ins$$anonymous$$d to having it in every scene. so when button is pressed, function from ads gameobject is called.
like you mentioned it should find it once the scene is loaded, but as it shows error of cant covert void to gameobject, unity doesnt play the scene untill all errors are fixed, so this way doesnt work.
Answer by AdityaViaxor · Sep 04, 2018 at 09:51 AM
You can Not call the gameObject that is in another Scene if want to test it do one thing make game object public and give reference in Inspector and now open the scene and change scene you will find that the object that you assigned in inspector from scene is gone and showing missing. so what you can do is make prefab of object and assign to script.
What you are talking about is that cross-scene references are not supported in the Editor, i.e. you cannot assign a game object or component from one scene to a serialized field of another scene from the Editor.
However, programatically, you can definitely access game objects and components from different scenes.
For example try this:
public class PersistentObject : $$anonymous$$onoBehaviour {
public static PersistentObject instance; // this is a crude singleton
private void Awake () {
if (instance == null) {
instance = this;
DontDestroyOnLoad(gameObject);
}
else { Destroy(gameObject);
}
public void SetObject (GameObject value) {
Debug.Log("PersistentObject " + name + " had SetObject() called on it with parameter " + value.name);
}
}
public void Test : $$anonymous$$onoBehaviour {
private void Start () {
if (PersistentObject.instance != null) { PersistentObject.Instance.SetObject(gameObject); }
}
}
Put these 2 scripts onto separate objects in a scene, then press play. Due to DontDestroyOnLoad()
the game object that has PersistentObject
on it will be moved to a special "DontDestroyOnLoad" scene, but the Test
script will still be able to access it (through script).