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.
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.
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.
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.