Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hi_No_Te · May 06 at 05:22 AM · strings

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hi_No_Te · May 06 at 05:24 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hi_No_Te · May 06 at 12:36 PM 0
Share

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.

avatar image Hi_No_Te · May 06 at 01:11 PM 0
Share

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));
 }
avatar image Hi_No_Te · May 06 at 01:13 PM 0
Share

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)

avatar image Hi_No_Te · May 06 at 01:13 PM 0
Share

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)

avatar image Hi_No_Te · May 06 at 01:13 PM 0
Share

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)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Component With Variable Help 2 Answers

Javascript Strings. check for number of strings in strings? 1 Answer

Creating High score using GUIText, '>' operation no working 1 Answer

Using string to refer to variable 2 Answers

Finding phrases within strings 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges