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

Resolved Main script has lost all object references, how do I get them back?

Discussion in 'Scripting' started by AliumOnions, Aug 15, 2023.

  1. AliumOnions

    AliumOnions

    Joined:
    Jul 9, 2012
    Posts:
    6
    Ok, first up, I'm not sure I'm calling this by the correct terms, so I'll start with defining that just in case I'm using it wrong.

    When you put a variable declaration on a script, for example:

    public GameObject mainMenuObj;

    And then inside Unity Editor when you click into Projects > Assets > Scripts, and open the script in the Inspector, and in the Inspector you see a little box that says None next to the variable you declared. Is that called a reference? Is it called a link? I can't seem to get any search results back when I try to find anything with it, so anyway, I'm calling it a reference, and now that I've defined what I mean hopefully people can correct me if wrong and/or understand the rest of my question.

    == OK! Actual Question: ==
    My main script has lots and lots of object references, and I recently opened the main script in the Inspector to see what object/prefab I was referencing for a specific function, and I found to my dismay that ALL of the boxes that used to have valid references/links all now say None, i.e. they have seem to be unlinked somehow. However, when I check it in Play mode, everything works still and acts as if all the references are still there and valid.

    Are the references still there but not displaying in the Inspector for some reason? Is there a way to get them back, i.e. by loading a previous saved version of this scene with the same script? I wrote most of this script last year and haven't touched it since, so I don't remember much of it and I'm not looking forward to the prospect of recreating all my object references.

    Any help and/or answers would be greatly appreciated!
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    If that's the case, are you sure that you're actually using the values assigned in the inspector? Maybe you are assigning them in your code (like with getComponent for example?)
     
  3. AliumOnions

    AliumOnions

    Joined:
    Jul 9, 2012
    Posts:
    6
    Well I don't see tons of lines of getComponent, so I don't think I'm assigning them in my code. How would I be able to check? (Other than looking for getComponent lines?)
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    did you use source control?
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    This is not a script that you wrote yourself? Is it something you could post here?
     
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    By the way you word this, sounds like your looking at the script(prefab), which would be empty as it's just a project reference. You more than likely have this script on a "master" object somewhere in your scene hierarchy, with all of the object references filled in.
     
  7. AliumOnions

    AliumOnions

    Joined:
    Jul 9, 2012
    Posts:
    6
    Not sure what you mean by source control. Do you mean version control? I just manually save the scene as a new name every so often because I didn't feel like learning a new VC.

    Yeah I wrote it myself, but I wrote it last year so I don't remember much of it anymore haha. It's basically like a learning exercise. Sure I can post it here, just be forewarned that's it's very long and sloppy. Is it better to copy/paste it as text in the body of a forum post or upload it as a file?
     
  8. AliumOnions

    AliumOnions

    Joined:
    Jul 9, 2012
    Posts:
    6
    Aha! Yes, you are correct. I see the references on the object where I've attached the script. Thank you so much!
     
    wideeyenow_unity likes this.
  9. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    I use git, if i like the code i keep it, if I dont i reset it.. thats it.. isnt much to learn.. especially with the gui clients for it
     
  10. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    In the text window you type in for the post, there's a code button, choose the first one, as that is designed for many lines of code. the second button is more for one liners.
     
  11. AliumOnions

    AliumOnions

    Joined:
    Jul 9, 2012
    Posts:
    6
    Ah yeah I was using Git at one point, but I'm on Linux and the client I had isn't supported anymore, and so I just went to the most basic for now haha.

    Ok, I'll use the code insert. Thanks!
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    The references you see when you click on the script itself are called the Script Defaults. They don't really mean much except they'll be assigned to any new instances of that component you create in the editor.

    Make sure you understand the difference between script defaults and the references you've established on an actual instance of the script in your scene.