Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by acclogin71 · Sep 04, 2018 at 04:13 AM · scenefunctiondontdestroyonloadanother

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.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · 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 acclogin71 · Sep 05, 2018 at 09:00 AM 0
Share

thanks for the reply but gameobject is in another scene.

avatar image misher acclogin71 · Sep 05, 2018 at 09:03 AM 0
Share

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.

avatar image Harinezumi misher · Sep 05, 2018 at 09:09 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 5 · 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 acclogin71 · Sep 04, 2018 at 09:34 AM 0
Share

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.

avatar image Harinezumi acclogin71 · Sep 04, 2018 at 09:48 AM 0
Share

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.

avatar image acclogin71 Harinezumi · Sep 04, 2018 at 10:50 AM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · 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 Harinezumi · Sep 04, 2018 at 10:18 AM 0
Share

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

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

110 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

Related Questions

"Main" function? 3 Answers

Select object in another scene 1 Answer

How do I transfer a gameobject created in one scene to another scene AND use it in a c# script? 1 Answer

Call a Function from another Script 1 Answer

Facebook posting problem in multiple scene architechture 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