- Home /
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. :)
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612050906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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