- Home /
Putting together String with Parameter in Function
First off, thank you for those coming to try to help - I have been lost, confused and scratching my head at this for hours, and after diving through Unity and Microsoft VS forums, I am still at a lost for my conundrom. I am attempting to add/append the parameter/variable string with another non-variable string, but it doesn't seem to work. The combination of these strings should result in the name of a scene that loads in the decorations. No matter what, adding past the parameter doesn't do anything.
The code:
public override void GoToArena(string sceneName) { print(" --- Client GoToArena ---"); StartCoroutine(LoadArena(sceneName)); }
IEnumerator LoadArena(string sceneName)
{
yield return StartCoroutine(LoadSceneByName(sceneName, LoadSceneMode.Single));
string arena = "";
arena += sceneName;
arena += "Decor";
string two = "";
two += "Decor";
two += "help";
string three = "";
three += "Decor";
three += sceneName;
string four = "";
four += "Decor";
four += two;
string five = " ";
five += sceneName;
five += "Decor";
five = five.Trim(); //five.Replace(' ', String.Empty);
Debug.Log(arena);
Debug.Log(two);
Debug.Log(three);
Debug.Log(four);
Debug.Log(five);
Debug.Log(addToString("Help"));
//yield return StartCoroutine(LoadSceneByName(five, LoadSceneMode.Additive));
yield return StartCoroutine(LoadSceneDecorByName(sceneName));
print(" --- Messenger reporting back - " + Messenger);
Messenger.ClientCall_FinishLoadingArena(sceneName);
}
IEnumerator LoadSceneByName(string scene, LoadSceneMode loadingMode)
{
print(" --- Loading scene: " + scene);
AsyncOperation loadScene = SceneManager.LoadSceneAsync(scene, loadingMode);
while (!loadScene.isDone)
yield return null;
}
IEnumerator LoadSceneDecorByName(string scene)
{
print(" --- Loading scene: " + scene + "Decor");
Debug.Log(" --- Loading scene: " + scene + "Decor");
Debug.Log(String.Format("{0}Decor", scene));
Debug.Log($"{scene}Decor");
Debug.Log(addToString(scene));
AsyncOperation loadScene = SceneManager.LoadSceneAsync(scene + "Decor", LoadSceneMode.Additive);
while (!loadScene.isDone)
yield return null;
}
string addToString(string one)
{
return one + "Decor";
}
All of the messiness are my attempts to figure out a culprit, but finding out, nothing helps once the parameter is used. I will post the console/prints in a comment. Again, I appreciate any help; maybe I am missing something, and hopefully this will help anyone else who manages to run into the same issue.
Console, respective to each print/Debug.Log: --- Loading scene: LevelOne
LevelOne
Decorhelp
DecorLevelOne
DecorDecorhelp
LevelOne
HelpDecor
--- Loading scene: LevelOne
--- Loading scene: LevelOne
LevelOne
LevelOne
LevelOne
Answer by Bunny83 · May 06 at 07:32 AM
First of all your debugging work is horrible :)
You just threw 5 methods (3 of them are coroutines) at us without any information which methods are actually called. Furthermore you just printed out a single string without any prefix so you can actually correlate a debug output with a certain debug statement. So it took a while to make any sense of this mess. For the future, please use descriptive log strings.
So, about your problem. As you said yourself, the issue always arises when you use your "sceneName" variable. You've done several tests and it always happens when you use the sceneName variable. However as you can see, you can add the scene name after some other string, but you can not add anything after the sceneName itself. This shows us that your sceneName most likely has a null character at the end. We don't know where your sceneName comes from since that's a parameter of your method. However it most likely contains a null character that is not displayed in the console.
Keep in mind that Unity itself and the core of the Unity editor is written in C++. That means a null character inside a string will be interpreted as a null terminating character by C++. The appended text is actually there in the C# string variable, but it's not shown in the console. Of course scene loading does not work, if your string contains unwanted characters.
Since we don't know where your string comes from, you have to investigate why there may be an illegal character in your string.
If I have the suspicion that the string may contain invisible / unwanted characters, I first print out the length of the string. This often times already reveals there's something more. If I want to know which characters, I usually convert the string to a byte array and print out the numeric values of the string
For example:
public static string DebugString(string aText, string aFormat = "{0:D}")
{
// alternative format as hex numbers: "0x{0:X}"
var sb = new System.Text.StringBuilder();
sb.Append("debugString: ").Append(aText.Length).Append(" = [");
for(int i = 0; i < aText.Length; i++)
{
if (i > 0)
sb.Append(", ");
sb.AppendFormat(aFormat,(int)aText[i]);
}
sb.Append("] : >").Append(aText).Append("<");
return sb.ToString();
}
So try to print the result of this method when you put your sceneName through Make sure you count the characters and compare them to the length displayed. Your string "LevelOne" should have 8 characters if there are no additional characters.
Thank you for your non-constructive criticism. Like my debugging, you tell me there's a problem with little assistance to improve... but criticism is criticism. This is probably my second or third post ever, and I was trying to keep it concise, especially since I don't know how to make my question as need and well organized as everyone else I've seen post here.
Anyways, I very much appreciate the information, as I would have never have known about the null value ending a string, as I have never remember dealing with it in my 10 years of amateur coding. I will attempt a couple more things and then post the results and update (providing the additional code) on this problem. Thank you for your time.
Adding additional functions for their calls:
On Script "NetworkMessageHandler":
private void Awake()
{
CustomMessagingManager.RegisterNamedMessageHandler("LoadScene", LoadSceneOverNetwork);
}
string StreamToText(Stream stream)
{
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
if (text == null)
return "";
return text;
}
Stream TextToStream(string text)
{
byte[] byteArray = Encoding.ASCII.GetBytes(text);
MemoryStream stream = new MemoryStream(byteArray);
return stream;
}
private void LoadSceneOverNetwork(ulong sender, Stream payload)
{
string arenaSceneName = StreamToText(payload);
print("Scene Name from payload: " + arenaSceneName);
print("Length: " + arenaSceneName.Length);
foreach (char c in arenaSceneName)
{
string sC = char.ToString(c);
if (string.IsNullOrEmpty(sC) || string.IsNullOrWhiteSpace(sC))
{
print("- In arenaName, character " + arenaSceneName.IndexOf(c) + " is null.");
}
}
arenaSceneName = arenaSceneName.Trim();
print("Scene Name from payload after trim: " + arenaSceneName);
print("Length after: " + arenaSceneName.Length);
BaseConnectionScript.Instance.GoToArena(arenaSceneName);
}
In Script "ClientConnectionManagementScript" (inherents from BaseConnectionScript):
public override void GoToArena(string sceneName)
{
print(" --- Client GoToArena ---");
StartCoroutine(LoadArena(sceneName));
}
Console print out (I am only including the console print out of the above functions)
Scene Name from payload: LevelOne UnityEngine.MonoBehaviour:print (object) NetworkMessageHandler:LoadSceneOverNetwork (ulong,System.IO.Stream) (at Assets/Scripts/New Network/NetworkMessageHandler.cs:86) MLAPI.Messaging.CustomMessagingManager:InvokeNamedMessage (ulong,ulong,System.IO.Stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/CustomMessageManager.cs:105) MLAPI.Messaging.InternalMessageHandler:HandleNamedMessage (ulong,System.IO.Stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/InternalMessageHandler.cs:607) MLAPI.NetworkManager:HandleIncomingData (ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1,single,bool) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:1028) MLAPI.NetworkManager:HandleRawTransportPoll (MLAPI.Transports.NetworkEvent,ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1,single) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:875) MLAPI.NetworkManager:OnNetworkEarlyUpdate () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:683) MLAPI.NetworkManager:NetworkUpdate (MLAPI.NetworkUpdateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:641) MLAPI.NetworkUpdateLoop:RunNetworkUpdateStage (MLAPI.NetworkUpdateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:148) MLAPI.NetworkUpdateLoop/NetworkEarlyUpdate/<>c:b__0_0 () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:171)
Length: 5114 UnityEngine.MonoBehaviour:print (object) NetworkMessageHandler:LoadSceneOverNetwork (ulong,System.IO.Stream) (at Assets/Scripts/New Network/NetworkMessageHandler.cs:87) MLAPI.Messaging.CustomMessagingManager:InvokeNamedMessage (ulong,ulong,System.IO.Stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/CustomMessageManager.cs:105) MLAPI.Messaging.InternalMessageHandler:HandleNamedMessage (ulong,System.IO.Stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/InternalMessageHandler.cs:607) MLAPI.NetworkManager:HandleIncomingData (ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1,single,bool) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:1028) MLAPI.NetworkManager:HandleRawTransportPoll (MLAPI.Transports.NetworkEvent,ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1,single) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:875) MLAPI.NetworkManager:OnNetworkEarlyUpdate () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:683) MLAPI.NetworkManager:NetworkUpdate (MLAPI.NetworkUpdateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:641) MLAPI.NetworkUpdateLoop:RunNetworkUpdateStage (MLAPI.NetworkUpdateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:148) MLAPI.NetworkUpdateLoop/NetworkEarlyUpdate/<>c:b__0_0 () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:171)
Scene Name from payload after trim: LevelOne UnityEngine.MonoBehaviour:print (object) NetworkMessageHandler:LoadSceneOverNetwork (ulong,System.IO.Stream) (at Assets/Scripts/New Network/NetworkMessageHandler.cs:99) MLAPI.Messaging.CustomMessagingManager:InvokeNamedMessage (ulong,ulong,System.IO.Stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/CustomMessageManager.cs:105) MLAPI.Messaging.InternalMessageHandler:HandleNamedMessage (ulong,System.IO.Stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/InternalMessageHandler.cs:607) MLAPI.NetworkManager:HandleIncomingData (ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1,single,bool) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:1028) MLAPI.NetworkManager:HandleRawTransportPoll (MLAPI.Transports.NetworkEvent,ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1,single) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:875) MLAPI.NetworkManager:OnNetworkEarlyUpdate () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:683) MLAPI.NetworkManager:NetworkUpdate (MLAPI.NetworkUpdateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:641) MLAPI.NetworkUpdateLoop:RunNetworkUpdateStage (MLAPI.NetworkUpdateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:148) MLAPI.NetworkUpdateLoop/NetworkEarlyUpdate/<>c:b__0_0 () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:171)
Your answer
