ScenePersist.CS causing coins to not render

So the problem I’m having in Tile_Vainia is that when I add the ScenePersist.cs component the coins (or really whatever gameObject has this script attached to it) does not render when I hit play in unity. They’re visible in scene & game view. The coins are not invisible when I hit play they simply are not existing in my game. Below is my full code as I’ve tried to use the ScenePercist.CS from 2018 & 2021 Tile_Vainia but got the same results.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ScenePersist : MonoBehaviour {/*

int startingSceneIndex;


private void Awake()
{
    int numScenePersist = FindObjectsOfType<ScenePersist>().Length;
    if (numScenePersist > 1)
    {
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }
}

// Use this for initialization
void Start () {
    startingSceneIndex = SceneManager.GetActiveScene().buildIndex;
}

// Update is called once per frame
void Update () {
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    if (currentSceneIndex != startingSceneIndex)
    {
        Destroy(gameObject);
    }
}*/
void Awake()
{
    int numScenePersists = FindObjectsOfType<ScenePersist>().Length;
    if (numScenePersists > 1)
    {
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }
}
public void ResetScenePersist()
{
    Destroy(gameObject);
}

}

Hi David,

Check if your “singleton” calls gameObject.SetActive(false); in the same if-block where Destroy(gameObject); gets called. If there isn’t that line of code, add it.


See also:

Ok. Also when I checked the solution I was no longer to thank you @Nina @MichaelP for helping me with the main issue. I truly appreciate your help.

public void MainMenu()
{
//Loading Main Menu will destroy the gameObject even if dontDestroyOnLoad from any scene
SceneManager.LoadScene(1);
var killThis = FindObjectOfType();

    if (killThis != null) Destroy(killThis);
    FindObjectOfType<Levels>().ResetGame();
   FindObjectOfType<GameSession>().KillMe();
    Destroy(gameObject);
} ....

I’m upset because I didn’t post at the end incase someone else has the same issue & wants a solution & don’t want to wrestle like we just did.
The game is publishable now, things like ScenePersists.cs are really spice. The soundtrack is amazing. I now know to write my comments before I check solved. The code M.P. added to my code makes gameObjects suicidal when it’s time to die.
Ok back to checking my singleton because I know that the coins rendered before we began destroying things that are dontDestroyOnLoad.
Again thanks for your help on that you 2. M.P. is THE MAN!!!

1 Like

Good job! And thanks for having been so patient with us. It can be really difficult to communicate one’s problem with other people, especially on the internet. I’m glad you finally solved the problem, and I hope it felt rewarding as you had invested so much time in it. :slight_smile:

Regarding the issue with the coins, try what I suggested above. It’s just one line of code. And if that didn’t work, feel free to test the code I posted in your other thread. As I told you in the other thread, I renamed the classname. The original name was ScenePersist. :wink:

1 Like

I’m sure what you’re telling me is simple & will solve this issue but to be perfectly honest I don’t get what you mean (this seems to happen between us). I did try changing the class name but nothing changed in the game.

Nothing is supposed to change in the scene. The code is supposed to solve the following problem: If a new scene gets loaded which is not the scene at startingSceneIndex, we destroy the object. And if we have a duplicate in the scene, we make sure it cannot be found with the FindObjectOfType method, and we destroy it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ScenePersist : MonoBehaviour
{
    int startingSceneIndex;

    private void Awake()
    {
        int numScenePersist = FindObjectsOfType<ScenePersist>().Length;
    
        if (numScenePersist > 1)
        {
            gameObject.SetActive(false); // <-- my edit
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }

    // Use this for initialization
    void Start ()
    {
        startingSceneIndex = SceneManager.GetActiveScene().buildIndex;
    }

    // Update is called once per frame
    void Update ()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

        if (currentSceneIndex != startingSceneIndex)
        {
            gameObject.SetActive(false); // <-- to be on the safe side, we do the same here
            Destroy(gameObject);
        }
    }

    public void ResetScenePersist()
    {
        Destroy(gameObject);
    }
}

To avoid any misunderstandings, I edited the code you posted in this thread. See the lines with gameObject.SetActive(false); in Awake() and Update(). Did you test this? The best would be to simply copy and paste the code.

Ohhh. I kinda think I understand what your edit is supposed to do. After testing it, making sure my sorting layers were correct, moving the coins around the hierarchy what I found is that nothing has changed.
So I have 1 coin that does not have ScenePersist.cs on it & that scene renders in the game when I hit play. Every other coin in the game has ScenePercist.cs component to the coin & even when I go to the prefab & mute the script on the coins they don’t appear in the game (after hitting play) unless I totally remove ScenePercist.cs from the game object.

I’m not sure if I understood this correctly but does each coin have a ScenePersist component attached?

If that’s the case, it’s no surprise that your code is not working as expected because every game object with a ScenePersist component but 1 gets destroyed. See here:

        int numScenePersist = FindObjectsOfType<ScenePersist>().Length;
    
        if (numScenePersist > 1)
        {
            gameObject.SetActive(false);
            Destroy(gameObject);
        }

There must be only one ScenePersist object in the scene. Ideally, it should be attached to the root game object whose children are not supposed to get destroyed.

Thanks so much Nina I did try that. It didn’t work. Making game is insane. I have things that did work & all of a sudden (at least to me don’t). Let me get to the point. There’s more issues with Tile_Vainia I’m having with the score & the coins & this is what I’m going to do about them-nothing. Because of the glitches there’s what I call the “Tile_Vainia Challenges” which give my Tile_Vainia replay value. I rendered a windows/mac version of the game that works the way I planned it to (even with all the glitches & problems). I managed to make the glitches part of the game that the players may or may not notice as glitches but part of their experience.
Here’s where I’m at now. The WebGL render is not giving me what I desire I mean. When I render a WebGL I get one of these outcomes depending on my publishing settings.

  1. The index file does not load at all in any web browser. 2. When I render the game the game works but none of the CSs work & one of the CSs causes black screen that never ends (yes I have Cut Scene, animations & ending credits). 3. The index renders but the canvases are incorrect in size & the Cut Scenes don’t render.
    The video screen in the Congratulations/Game over Stages cause problems covering parts of the canvas so I can’t see some of the canvas & any of that video/animation.
    The Windows/Mac game has no problems & looks beautiful & the soundtrack works perfectly.

I guess i mean video clips in general.

Here are a few suggestions for the problems with the build itself:

  1. Save your build on your harddrive but not inside your project folder, on your Desktop or in a cloud.
  2. Use English characters (A-Z, a-z, 0-9) only for your file and folder names.
  3. Remove all scenes from the Build Settings list and readd them.
  4. Make sure that neither your firewall nor your antivirus program block Unity.
  5. Run Unity with administrator rights.
  6. Update Unity to a newer stable version.

Also make sure that your scenes are in the correct order in the list in Build Settings. And double check the name of your scenes. Another student recently was not able to find the problem because they tested some sandbox scene in the editor. That scene looked very similar to their actual level 1. And they (and I) wondered why nothing worked in the build as seen in the editor. It took us many hours to spot the actual issue. The entire time, the student’s project was working perfectly fine but due to a misunderstanding (the wrong scene opened in the editor), we thought there was some crazy bug.

Long story short, double check everything, especially the “silly” things.

Regarding the canvas, make sure its Render Mode is set to “Screen Space - Overlay”, so it adjusts itself to different screen sizes. If you know that your game will be displayed in a rather small area, set a small resolution in the game window to see if your game still looks fine. In the browser, your available space is cropped by many things.

Thanks Nina. I’ll get on that soon. I was playing the windows version & it’s really perfect.
On a personal note I’d love to be able to post this game because of the many people (like yourself, Michael P., SuperJustin5000 & others) who have helped me & ya’ll can see what ya’ll helped accomplish. Someone suggested using a webGL asset from the store. I’d definitely purchase one if that could just make this work. Rick said some important things about my future & I need to walk in that path.

I’d really love to see your game. :slight_smile:

Regarding the WebGL asset, for what do you need that? Unity has got its own module which you could use to build to WebGL. Is there a specific problem the creator of that asset promised to solve?

My personal advice is to not buy or collect stuff unless you know that it will solve problems for you (mainly time). Otherwise, you’ll end up with lots of things that do not help you at all, and less money. I made this “mistake” myself many, many times. “Oh, this Blender tool is just $50 and on sale. I’m buying this because I need this for reasons.” I recently remembered that I had bought said tool like a decade ago. I am not even sure if I ever downloaded it but I do remember that I never installed it. Also, I don’t remember its name, so I cannot even find it again without research. Those $50 definitely were no investment in my future but when I purchased that tool, it felt like they were.

Long story short, be honest with yourself and be “wary” because there are so many interesting things out there that look helpful at first glance but will not help you at all.

Ha I did that with playmaker & used it literally once only to find out the assets were outdated. $65 wasted. Thank you for sharing your story I know I’m not alone in falling for those sales that are totally useless to me but look great.
I think I should end this & actually start a topic based on the web GL issue because it seems a lot of people are having issues with webGL & unity in general. In regards to the issues I’m not going to solve Rick updated Tile_Vainia using Input Systems. I can do Tile_Vainia over with the knowledge I have. I have a vision for my next game based on Tile_Vainia. So I technically can’t say these issues are resolved but as far as I’m concerned you can close them out.
Thank you so very much miss Nina.

Guilty too! Those humble/Unity bundles are hard to pass up.

Privacy & Terms