Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Anonymous 2 · Feb 04, 2011 at 12:40 AM · stopexecute

Why this code does not execute?

I have this code inside loop and it stops executing when it reaches to point where it logs "Started player handling", what is wrong?

using UnityEngine; using System.Collections; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading;

public class PlayerUpdaterCS : MonoBehaviour {

public GameObject connectivity; public GameObject gamer; public string[] existingplayers; private Transform transformer; public bool connected; Thread receiveThread;

// Use this for initialization void Start () { init(); }

private void init() { receiveThread = new Thread(new ThreadStart(PlayerUpdate)); receiveThread.IsBackground = true; }

private void PlayerUpdate() { while(true) { Debug.Log("did i get here?"); string message = Messaging.ReceiveMessage(); HandleMessage(message); } }

private void HandleMessage(string message){ string[] osat = message.Split('>'); string playeridS = osat[0].Replace("ID:", ""); int playerid = int.Parse(playeridS); string playername = osat[1].Replace("N:", "");

     string playerxyz = osat[2];
     //muuttaa xyz arvot
     string[] xyz = playerxyz.Split('Y');
     float x = float.Parse(xyz[0].Replace("X:", ""));
     string[] yz = xyz[1].Split('Z');
     float y = float.Parse(yz[0].Replace(":", ""));
     float z = float.Parse(yz[1].Replace(":", ""));
     Vector3 sijainti;
     sijainti.x = x;
     sijainti.y = y;
     sijainti.z = z;
     Debug.Log("Finished with message handling");
     HandlePlayers(sijainti, playerid, playername);

}

private void HandlePlayers(Vector3 sijainti, int playerid, string playername) { Debug.Log("Started player handling"); GameObject otherplayer; if(existingplayers[playerid] == playername) { Debug.Log("did i get here?5"); otherplayer = GameObject.FindWithTag ("OtherPlayer"+playerid); otherplayer.transform.position = sijainti; Debug.Log("did i get here?6"); } else { Debug.Log("did i get here?3"); existingplayers[playerid] = playername; otherplayer = Instantiate(gamer, sijainti, Quaternion.identity) as GameObject; otherplayer.tag = "OtherPlayer"+playerid; Debug.Log("did i get here?4"); } }

void Update () { if(Messaging.isConnected == true && connected == false) { connected = true; receiveThread.Start(); } }

}

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 yoyo · Feb 04, 2011 at 12:45 AM 0
Share

If existingplayers is null or playerid is greater than the length of existingplayers then the code will thrown exception. Try running in the $$anonymous$$onoDevelop debugger, or add more Debug.Log messages to see the values of existingplayers, playerid, etc.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Feb 04, 2011 at 12:47 AM

You can't use any Unity API in threads, only Mono API.

Comment
Add comment · Show 3 · 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 Anonymous 2 · Feb 04, 2011 at 12:54 AM 0
Share

Is there any workarounds for threading then? Somehow without threading the game started passed one frame after it had recieved message from server.

avatar image Eric5h5 · Feb 04, 2011 at 01:41 AM 0
Share

Possibly coroutines.

avatar image CHPedersen · Apr 07, 2011 at 07:49 PM 0
Share

Threads do allow calls to Debug.Log. Other than that, true, no Unity API.

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

No one has followed this question yet.

Related Questions

Stop Script Completely. Doesn't Execute Another Line Of Code. 1 Answer

How to make a pause function? 3 Answers

animation on multiple objects 1 Answer

Stop audio loop 1 Answer

Keeping audio between all but one scene? 1 Answer


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