I’ve attached a copy of the script to get a better idea of what I’m trying to do.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Play : MonoBehaviour {
public Text scoreText;
public Text messageText;
public int scoreValue;
public static int score;
void Start ()
{
score = 0;
UpdateScore ();
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
if (score >= 30)
{
messageText.text = "Congratulations you are moving to the next level!";
StartCoroutine ("Wait");
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds (5f);
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex + 1);
}
if (SceneManager.GetActiveScene().name == "Scene 5")
{
messageText.text = "Look for oversized bushes, a ladder stored in the wrong place and a light that's not working.";
}
@callen is right, you’re being unclear about the scenario. I saw that the if statement is outside of any method or function but I guess you’re trying to show what you want to work? Also if you’re checking the scene name after loading the scene you might want to consider putting a script in the scene you’re loading. Otherwise it won’t work.
For example:
LoadSceneScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadSceneScript : MonoBehaviour {
public Text scoreText;
public Text messageText;
public int scoreValue;
public static int score;
void Start ()
{
score = 0;
UpdateScore ();
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
if (score >= 30)
{
messageText.text = "Congratulations you are moving to the next level!";
StartCoroutine ("Wait");
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds (5f);
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex + 1);
}
}
OnScene5LoadScript.cs (Put this on a gameobject in Scene 5)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class OnScene5LoadScript : MonoBehaviour {
public Text messageText;
void Start ()
{
if (SceneManager.GetActiveScene().name == "Scene 5")
{
messageText.text = "Look for oversized bushes, a ladder stored in the
wrong place and a light that's not working.";
}
}
I hope this somewhat helps.
Callen, Thanks for your response. The messages I’m getting at line 12 in the script is "Unexpected symbol ‘if’ in class, struct, or interface member declaration. It also includes the same message for ‘(’ and ‘==’. The message I get on line 13 is “Primary constructor body is not allowed”.
I’m new to C# and will appreciate your thoughts and suggestions. What I an wanting to do with the if statement is to add unique messages for each of my 8 scenes in this game I’m building.
All the best,
Bill