Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why Does My Game Object Loose Connection With Variable

Discussion in 'Scripting' started by KnightRiderGuy, Jan 6, 2016.

  1. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    Ok This is something that has been driving me nuts for what feels like forever, I have asked this before but got basically just the run around.

    OK I have an empty game object that I'm using as kind of a control object that has references to things like a UI slider & Text Objects on a canvas, My game object has the following do not destroy on it:

    Code (CSharp):
    1. void Awake () {
    2.         if (sending == null) {
    3.             DontDestroyOnLoad (gameObject);
    4.             sending = this;
    5.         } else if (sending != this) {
    6.             Destroy (gameObject);
    7.         }
    8.     }
    Now when I exit this screen and then come back the most annoying thing happens. it looks connection to the UI slider and UI text. How the bloody hell do I fix that?

    Screen Shot 2016-01-06 at 11.13.41 AM.png
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Either find them again, using OnLevelWasLoaded or similar. Or make the referenced objects dont destroy as well.
    Loading a scene doesnt magically fix this, it creates new objects that may look like they are the same, but they are not.
     
    Magiichan likes this.
  3. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1.     void Awake () {
    2.         if (sending == null) {
    3.             DontDestroyOnLoad (gameObject);
    4.             sending = this;
    5.         } else if (sending != this) {
    6.             if (this.lightSlider != null && sender.lightSlider == null) {
    7.                 Destroy (sender.gameObject);
    8.                 sender = this;
    9.             } else {
    10.                 Destroy(gameObject);
    11.             }
    12.         }
    13.     }
    This checks if the sender instance has references to the components in the scene(using the lightSlider variable as example) and if it doesn't have the variables referenced anymore, it'll replace the old GameObject by the new spawned one.
     
    Last edited: Jan 6, 2016
    ThermalFusion likes this.
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I would recommend doing it the other way around if you have other information stored in the old "singleton" that you need to keep.
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    That's a posibility, the best way to keep information stored without dependancy of a component would be static variables though. So that the data is kept throughout every scene without having the transfer any variables.

    If we'd use your method we'd have to manually transfer every single variable. or use reflection which isn't ideal.
     
  6. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    I tried replacing my awake with that one and I get the same missing results in the inspector?
     
  7. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    This happens when I return to the main scene / original starting scene.I tried the method suggested by Magiichan and got the same missing items in the inspector?
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Do you dynamically load in the object in the scene, or is this empty gameobject always in the starting scene?
     
  9. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    It's always in the starting scene except when the scene changes I would assume it gets destroyed?
     
  10. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    The script makes sure the gameobject will not be detroyed when loading other scenes. Since your script is only holding variables for your starting scene, why would u want to keep it in every scene?

    Do you want the audiosource and the sliders to go to the other scene aswell?
     
  11. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    Nope the other screen is a music player and has it's own UI Canvas and is independent from scene one.
     
  12. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    So why do you make the script go to the other scenes with DontDestroyOnLoad(gameObject);?
     
    ThermalFusion likes this.
  13. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Was just about to ask, If you don't have features on this object that you need in every scene I can't really understand why you need it to keep around.
     
    Magiichan likes this.
  14. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    In post number 1 you will see I attached a screen grab of the inspector and the Hierarchy, What I need to have happen is that when I return back to the main scene from say either the music player or the Alpha Circuit test scene that my light bar works and the information is not lost between the text objects on the UI canvas. It seems that even though the "Sending" script has the don't destroy on it that even when it returns to the main screen it looses it's references to the UI Slide and Text.

    Basically I have those two items connected to a script that communicates with an Arduino connected to the Serial port. Here is a bit of a demo of the project, might not show the issue I'm having as I don't change scene in this demo but it does display the working of the Arduino being connected to the project.



    This 2nd video shows some of the external set up. ;)


    WOW! the ONKLy thing missing on this project is the Voice recognition and the Solenoid activated grappling hook launcher for K.I.T.T.s front nose, lol and yes I do mean a real grappling hook launcher ;)
     
  15. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    Oh yeah, the simple answer there Ivan is that the Sending script is connected to the Arduino, if I don't cary it over to other scenes then when it returns to the main scene it seems to loose it's connection with the Arduino. There might be a way to re open the Serial but I'm not sure how to do that. ;)
     
  16. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You should have the Arduino connection in a seperate 'manager' script that doesn't rely on referenced variables.
    Then make the scripts that use the variables request the data from the Arduino 'manager' script. The problem is that you only want to keep the Arduino connection alive, this doesn't have a direct corrolation with the UI so they should be 2 seperate scripts :)

    I mean, that becomes quite complicated when you dont have much coding knowledge, I might be able to help you out with that on a later date.
     
    KnightRiderGuy likes this.
  17. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    I think I know what you mean, Have another script that has the reference to the UI text and Slider and then just communicate to that script from the "Sending" script that is dong the communicating with the Arduino.
     
    Magiichan likes this.