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 MadJohny · Feb 26, 2014 at 03:53 PM · c#freeze

What could be freezing unity?

Hi, in my game you are supposed to play as a vampire, and I am adding the mechanic to morph into a bat for short periods of time, however, something is causing unity to freeze. The objective is simple in the script, there is one script that controls movement, and another script that controls the Player state (bat or vampire), but when I press the key to do it, unity freezes.

Movement Script:

 using UnityEngine;
 using System.Collections;
 
 public class VampireControllerScript : MonoBehaviour {
 
     CharacterController controller;
     bool morphed = false;
 
     float speed;
     public float flySpeed = 8.0F;
     public float runSpeed = 6.0F;
     public float walkSpeed = 3.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
 
     bool walking;
 
     public CameraTargetScript cameraTargetScript;
 
     void Start () {
         controller = GetComponent<CharacterController>();
     }
 
     void Update() {
         if (!morphed){
             if (Input.GetButton("Sneak")) {
                 if (controller.isGrounded) {
                     walking = true;
                 }
                 else {
                     walking = false;
                 }
             }
             else {
                 walking = false;
             }
 
             if (walking)
                 speed =  walkSpeed;
             else
                 speed = runSpeed;
             
             if (controller.isGrounded) {
                 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
                 moveDirection = transform.TransformDirection(moveDirection);
                 moveDirection *= speed;
 
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
                 
             }
 
             moveDirection.y -= gravity * Time.deltaTime;
             controller.Move(moveDirection * Time.deltaTime);
         }
         else {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= flySpeed;
             
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
             
             controller.Move(moveDirection * Time.deltaTime);
         }
     }
 
     void LateUpdate () {
         if (!morphed)
             transform.rotation = Quaternion.Euler (0f, cameraTargetScript.currentYRotation, 0f);
         else
             transform.rotation = Quaternion.Euler (cameraTargetScript.currentXRotation, cameraTargetScript.currentYRotation, 0f);
     }
 
     void Morph () {
         if (morphed)
             morphed = false;
         else
             morphed = true;
     }
 }

And the script to change state:

 using UnityEngine;
 using System.Collections;
 
 public class VampireScript : MonoBehaviour {
 
     CharacterController controller;
     public VampireControllerScript controllerScript;
 
     public float coolDown = 15f;
     public float duration = 5f;
     float startCoolDown;
     float startDuration;
 
     public float defaultHeight = 2f;
     public float morphedHeight = 1f;
     public float defaultYposition = 1f;
     public float morphedYposition = 0.5f;
 
     bool morphed;
 
     void Start () {
         controller = GetComponent<CharacterController>();
         startCoolDown = coolDown;
         startDuration = duration;
         coolDown = 0f;
     }
 
     void Update () {
         if (Input.GetButtonDown("Special Ability") && morphed) {
             Morph ();
         }
         if (Input.GetButtonDown("Special Ability") && !morphed) {
             if (coolDown <= 0f) {
                 Morph ();
             }
         }
 
         if (!morphed) {
             controller.height = defaultHeight;
             controller.center = new Vector3 (0f, defaultYposition, 0f);
             //default graphics
             coolDown -= Time.deltaTime;
             duration = startDuration;
         }
         else {
             controller.height = morphedHeight;
             controller.center = new Vector3 (0f, morphedYposition, 0f);
             //morphed graphics
             coolDown = startCoolDown;
             duration -= Time.deltaTime;
             if (duration <= 0f) {
                 Morph();
             }
         }
     }
 
     void Morph () {
         if (!morphed)
             morphed  = true;
         else
             morphed = false;
 
         controllerScript.SendMessage("Morph", SendMessageOptions.RequireReceiver);
     }
 }

So, do you know what could be freezing unity? Thanks in advance.

Comment
Add comment
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

2 Replies

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

Answer by Wolfram · Feb 26, 2014 at 04:10 PM

controllerScript.SendMessage() calls every method named "Morph" on every script attached to this GameObject. This includes itself, and as your calling method in VampireScript is allso called "Morph", you'll end up with an infinite loop. Rename one of your methods.

Comment
Add comment · Show 1 · 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 MadJohny · Feb 26, 2014 at 04:17 PM 0
Share

Okay, I tought that since I used controllerScript.Send$$anonymous$$essage it would only send the message to the controllerScript

avatar image
1

Answer by Owen-Reynolds · Feb 26, 2014 at 04:15 PM

An infinite loop is freezing you. The vampire Morph function is calling itself (with the sendMessage) over and over and over... . I don't use SendMessage and don't know what you're trying to accomplish, so can't suggest how to fix it (is the vampire morph trying to tell the vampControlScript to morph?)

For a test, add global int morphCount=0; and at the start of vampire morph add morphCount++; Debug.Log("mc="+morphCount); if(morphCount>=50) return;. It should blast out 1-50 morphs on a single keypress.

It is a real pain that an infinite loop in code freezes all of Unity, so you can't even click Stop. You just have to remember to SaveScene before running risky code.

Comment
Add comment · 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

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Flip over an object (smooth transition) 3 Answers

C# Drag rotation code crashing unity with no errors. 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