-1

I've tried adding a ) where it says to, but that adds more problems. The error I get states:

Assets/LevelComplete.cs(12,64): error CS1026: ) expected.

What I'm trying to do here is use the if statement to find out if the Active Scene is in the specific range. Also, for a little more context, All I need is a range, and not the code to pick a number in that range if that helps.

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LEvelComplete : MonoBehavior
{
    public void LoadNextLevel()
    {
        int Active12 = Range(1, 3);
        int Load34 = UnityEngine.Random.Range(3, 5);

        if (SceneManager.GetActiveScene().buildIndex = Active12
        {
            SceneManager.LoadScene(Load34);
        }
    }
}

2 Answers2

2
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using Random = UnityEngine.Random;

public class question_63947858_script_error : MonoBehaviour
{
    public void LoadNextLevel()
    {
        int Active12 = Random.Range(1, 3);
        int Load34 = Random.Range(3, 5);

        if (SceneManager.GetActiveScene().buildIndex == Active12)
        {
            SceneManager.LoadScene(Load34);
        }
    }
}

Things wrong:

  1. MonoBehaviour was spelled wrong
  2. System was clashing with UnityEngine namespace. So disambiguated with Random = UnityEngine.Random
  3. No parentheses after the if statement
  4. = changed to == in the if statement
Kale_Surfer_Dude
  • 884
  • 5
  • 10
1

"find out if the Active Scene is in the specific range."

...

var scene = SceneManager.GetActiveScene(); 
if (scene.buildIndex >= 1 && scene.buildIndex <= 3)

... or if you prefer Linq:

if (Active12.Contains(SceneManager.GetActiveScene().buildIndex))

See also How to elegantly check if a number is within a range?

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • I tried this and it said that "Non invocable member "scene.buildindex" cannot be used like a method – AgentGoodman Sep 18 '20 at 01:34
  • sorry that's just a typo on my part, it's a property so shouldn't have the brackets after it (which would be used for a method call). Try the updated version above. – ADyson Sep 18 '20 at 01:36
  • Thanks So much it worked. You don't know how much this is gonna help my game! – AgentGoodman Sep 18 '20 at 01:45