public int variable not updated in inspector for two scenes in Unity
I have five scenes in my Unity Project/Game. The first one is start scene (Build Index = 0) and remaining four are the levels of the game as (1,2,3,4). The logic is such that when the user moves from one scene to the next one, a new scene appears and previous scene is removed from the list. The order in which scenes appear is randomized. I did this by using a Singleton. This works perfectly fine. What I also want to do is, when a new scene is loaded the public int advancement = 8;
checks for the current scene. For example, when I press play and the run project starting from Scene 0, in the Inspector I see advancement = 8;
. I press button to load next scene. A new scene (lets say Scene 2) is loaded and in inspector I see advancement = 4;
as I proceed in the game the advancement
decreases as 3, 2, 1. This C# code works fine on my laptop. However, when I copied the the script in a different Unity Project on a different laptop. The code is working only for two scenes i.e., Scenes with Build Index 1 and 3. For Scenes with Build Index 2 and 4 the public variable advancement = 8
in the inspector and is never updated.
Can someone help me troubleshoot this?
Please see the code below.
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System;
public class BRIRToMax : MonoBehaviour {
private static int localPort;
private string IP; // define in init
public int port = 8051; // define in init
public int advancement = 8;
// "connection" things
IPEndPoint remoteEndPoint;
UdpClient client;
void Start()
{
IP = "127.0.0.1";
port = 8051;
remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
client = new UdpClient();
int currentSceneBuildIndex = SceneManager.GetActiveScene().buildIndex;
SendCurrentSceneBuildIndexToMaxMSP(currentSceneBuildIndex);
}
public void SendCurrentSceneBuildIndexToMaxMSP(int buildIndex)
{
// The UDP stuff to send to MaxMSP
byte[] data1;
Debug.Log("Build Index =:" +buildIndex);
if (GameObject.Find("ButtonNextScene"))
{
Debug.Log("Remaining Scenes = " + (SceneAdvance.instance.remainingScenes-1));
advancement = SceneAdvance.instance.remainingScenes;
}
try
{
if (advancement == 3)
{
byte[] welcome = Encoding.ASCII.GetBytes("H");
client.Send(welcome, welcome.Length, remoteEndPoint);
}
else
{
Debug.Log("Do not trigger the toggle");
}
switch (buildIndex)
{
case 1:
data1 = Encoding.ASCII.GetBytes("Q"); //Sc2_3500
client.Send(data1, data1.Length, remoteEndPoint);
Debug.Log("Q");
break;
case 2:
data1 = Encoding.ASCII.GetBytes("W"); //Sc2_1500
client.Send(data1, data1.Length, remoteEndPoint);
Debug.Log("W");
break;
case 3:
data1 = Encoding.ASCII.GetBytes("E");//Sc1_3500
client.Send(data1, data1.Length, remoteEndPoint);
Debug.Log("E");
break;
case 4:
data1 = Encoding.ASCII.GetBytes("R");//Sc1_1500
client.Send(data1, data1.Length, remoteEndPoint);
Debug.Log("R");
break;
}
}
catch (Exception err)
{
Debug.Log(err.ToString());
}
}
}