Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gothic_Anatomist · Dec 01, 2015 at 10:43 AM · crashfunctionfirstperson

Unity Crashing on Second function call

Hey Everyone, this is my first significant Unity project adventure and as you'll soon see I'm new at writing code.

It's basically a simple game where you roam an environtment, and when you select an object it brings up a screen where you can examine the object and have to answer a multiple choice question. It works well for the first object and the MCQ works as expected. When I get to the next object and select it (by pressing E) unity becomes unresponsive with no option but to 'end process' out of it. As such, there are no error logs to see why it failed.

Here are the bits of code involved in the object selection:

 using UnityEngine;
 using System.Collections;
 
 public class playerRayCast : MonoBehaviour {
     public float reachDistance;
     private Vector3 examine;
     public GameObject gameManagerController;
     public GameObject examineScreen;
     public GameObject examineCamera;
     private bool mainOnOff = true;
     private bool examineOnOff = false;
     RaycastHit whatIFound;
     public string qFromBP;
     public string[] ansFromBP;
     private GameObject foundObject;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Debug.DrawRay (this.transform.position, this.transform.forward * reachDistance, Color.magenta);
         if (Physics.Raycast (this.transform.position, this.transform.forward,out whatIFound, reachDistance)) {
 
 
             if (Input.GetKeyDown (KeyCode.E))
             {
 
                 Debug.Log ("I picked up a" +whatIFound.collider.gameObject.name);
                 if (whatIFound.collider.tag == "BodyPart")
                 {
                     //get question info
                     qFromBP = whatIFound.collider.gameObject.GetComponent<BodyPartAttributes>().qText;
                     ansFromBP = whatIFound.collider.gameObject.GetComponent<BodyPartAttributes>().answerText;
                     //call function in Game manager to change UI screens inputing bodypart information
                     gameManagerController.GetComponent<GameManager>().changeToExamine(whatIFound, qFromBP, ansFromBP);
 
                 }
             }
         }
     }
 
 }

This is the code used to populate the MCQ's with parameters passed from the above code: using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;

 public class QuestionManager : MonoBehaviour {
 
     public GameObject questionBox;
     public GameObject[] answerButtons;
     public GameObject answerPanel;
     public GameObject gameControl;
     private List<int> answersSpawned = new List<int>();
     private Text question;
     private static string[] answers;
 
     public void PopulateQuestion (string qText, string[] answerText) {
     
         for (int i = 0; i< answerButtons.Length;){
 
             GameObject aButton = answerButtons[i];
             Text thisAnswer = aButton.GetComponentInChildren<Text> ();
             question = questionBox.GetComponent<Text>() ;
             question.text = qText;
             answers = answerText;
 
             int answerIndex = Random.Range (0, answerButtons.Length);
             if (!answersSpawned.Contains (answerIndex)) {
 
                 thisAnswer.text = answers[answerIndex];
                 answersSpawned.Add(answerIndex);
                 i++;
 
             }
                 }
         }


and finally this is the prt of my 'GameManager' Script that chaanges the screens from the enironment to the question screen:

 public void changeToExamine(RaycastHit whatFound, string qTextGM, string[] ansTextGM){
 
         //turn on mouse cursor for selecting answers
         cursorShow = !cursorShow;
         Screen.showCursor = cursorShow;
         //switch from HUD view to examine screen
         mainOnOff = !mainOnOff;
         mainGame.SetActive(mainOnOff);
         examineOnOff = !examineOnOff;
         examineScreen.SetActive(examineOnOff);
         //move examineing camera to object found
         whatPlayerFound = whatFound;
         examine = whatPlayerFound.collider.gameObject.transform.position;
         float transportX = examine.x + 0.65f;
         float transportY = examine.y;
         float transportZ = examine.z -2.0f;
         examineCamera.transform.position = new Vector3(transportX , transportY, transportZ );
         //call function to populate question with parameters passed from body part
         qManagerObject.GetComponent<QuestionManager> ().PopulateQuestion (qTextGM, ansTextGM);
 
         
     }
     public void backFromExamine(){
         
         cursorShow = !cursorShow;
         Screen.showCursor = cursorShow;
         mainOnOff = !mainOnOff;
         examineOnOff = !examineOnOff;
         mainGame.SetActive(mainOnOff);
         examineScreen.SetActive(examineOnOff);
         whatPlayerFound.collider.gameObject.SetActive (false);
     }
     

Any advice as to why Unity is crashing on the second selection of an object would be very gratefully received! I just can't figure out why it works as expected on the first object, but crashes on the next one. :)

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 saschandroid · Dec 01, 2015 at 11:27 AM 1
Share

I could be wrong but at a quick look at your PopulateQuestion function I can see that you are increasing the i variable only in an if-statement. That means if for some reason the if-clause is not entered you can never leave the for-loop (because i< answerButtons.Length will always be true). That could be, if you enter the PopulateQuestion function a second time. The answerSpawned - list now probably contains all random answerIndex you'll ever get and can't leave the for-loop.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity Crash on Function Run 2 Answers

Waiting for a Function to Complete 1 Answer

[Closed]Unity crash, while/for loop 3 Answers

CharacterController not found the GameObject 1 Answer

Why won't Unity3d run? 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