Desperate for Help

Hey all,
I’m having a little trouble that I tried solving for 5-6 hours,
So I’ve created a fighter selection menu, basically its just a texture that player can choose, and I’m having trouble transferring this texture into next scene which is the game.
what I did is created same arrays of textures on both scenes, and the created empty object with Singleton method so it carries the index from first scene into game scene.
Now if I launch the game from the game scene, and by changing the index, the texture changes just fine, however if I launch the game from my plane selection screen, it seems like my empty object gets the index into the game scene, but somehow it doesn’t transfer to my player. it shows index as 0, even though the index on empty is not 0.
Any Ideas? I’m really tired now, of trying the whole day to find a solution, so if there is anyone to look into further I will add my scripts tomorrow

Hi Antanas,

Doesn’t sound like something that just posting the scripts will help to resolve to be honest, would be easier to see it in full context.

Could you share your project files, not a build, but the actual full project files, happy to take a look.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

1 Like

Good Afternoon,
Thank You for Your answer,
I just had to tidy up the script to make it as clear as possible, cause I’ve been changing and trying a lot of different methods,
I will try to explain a little, maybe that would save u some time,

Plane selection script is from texture selection screen, it works fine, u choose texture, and when u press button start (sceneLoader), script sends its index to Sellection script before it loads the game,
and this script has singleton method, so it carries the index to next scene, (till here it works fine),

and then player script should get the index from singleton and update its texture (i tried calling it on start, update and even awake), but it get only index 0, the strange thing is, that if i start game from game scene, and try changing the index in selection, it works fine.

So I would really appreciate if You would check it, I am like most of the people new to coding, so please don’t judge me, if there is some simple obvious mistake.

Here’s the link https://drive.google.com/file/d/1Pj8Z4RmhVJ1Ksk8BeEZli41YPK4UMd39/view?usp=sharing
Hope it works, I use it first time.

Hi Antanas,

You issue is with the singleton code you are using. I know this is from the course, but it isn’t ideal and contains a bug.

In this code;

private void SetUpSingleton()
{
    if (FindObjectsOfType<Sellection>().Length > 1)
    {
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }
}

The Destroy(gameObject) method is called, but the actual GameObject isn’t actually destroyed until the end of the frame, meaning that anything that may try to access it could, for the duration of that frame (see the link below with regards to execution / life cycle).

During this frame, the value is being taken as zero which is why your ship doesn’t change.

If you add a single line of code to that “singleton” code, the issue will be resolved;

private void SetUpSingleton()
{
    if (FindObjectsOfType<Sellection>().Length > 1)
    {
        gameObject.SetActive(false);    // add this line
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }
}

The above will disable the GameObject immediately, so even though it isn’t actually destroyed until the end of the frame it is no longer available to any other calling code.

Run your game now and you should find your issue resolved.

I wouldn’t do that.

There is some room for streamlining your code with regards to the plane selection. Also, you don’t need to set the texture over and over again in the Update method within Player.cs, once it’s set, it’s set. Move that to the Start method instead and you’ll have an instant performance increase :slight_smile:

Hope this helps :slight_smile:


See also;

1 Like

Wow,
You saved my week,
I took so long trying to figure out, (tbh its still a little bit confusing, but need to move on learning)
Thank You So much for Your help

1 Like

You’re very welcome :slight_smile:


See also;

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms