Forum rules - please read before posting.

Variable does not change in the script

I am changing a global variable in a custom script. Made a link to the custom script. I am writing this code:

               if (health <= 0)
                {
                    Destroy(gameObject);
                    AC.GlobalVariables.SetBooleanValue(1, true, false);
                    UnityEngine.SceneManagement.SceneManager.LoadScene("Home");
                }

The variable does not change. I understand correctly that this is a variable ID: prntscr.com/nk1ypf?

Comments

  • edited May 2019

    Your third parameter, synchronise, is set to false - which means it won't trigger the OnVariableUpload event as covered in the Manual's "Linking with custom scripts" chapter.

    It should still update correctly in the Variables Manager, however. Also note that you don't need to set a Variable's Link to field to Custom Script in order to set its value through script. This field is only necessary if you want it to sync automatically with an external script variable.

    Try running the command in isolation, i.e. without the LoadScene function call. To keep AC running correctly upon switching scene, you need to use the Scene: Switch Action, or AC's provided ChangeScene function:

    SceneInfo sceneInfo = new SceneInfo ("Home");
    KickStarter.sceneChanger.ChangeScene (sceneInfo, true);
    
  • edited May 2019

    OnVariableUpload should I call in my script? I bad understand this section in the manual :)
    prntscr.com/nka1c4
    Writes an error that a variable ID is not found. But right there in the log writes the value of this variable.
    prntscr.com/nka2dh
    Yesterday he didn't give such an error

  • Are you actually in an AC scene at the time you're calling this? AC Global Variables are inaccessible if your scene does not have an AC GameEngine in it.

  • AC GameEngine is there, but the scene is not configured. I don't use adventure creator in this scene. I need only one global variable here.

  • edited May 2019

    There was a thought. There are two scenes. First set up AC. The second without. If I record a variable in the second scene, for example, in the player prefs, then when I go to the first scene can I transfer the value from the player prefs to the AC global variable?

    P.S. It worked. But the global variable is not saved by itself when leaving the game. I made it load at the start of the scene from the player prefs. Not sure if this is correct. Perhaps in the future I will regret this decision, but so far everything is working.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.