Calling from another C# Script is casuing Unity 5.4 to crash in editor mode.
I have been making a very simple 2D game for a college project and have ran into an issue which does not make sense. This is my first ever attempt at Unity and C# so keep it simple. I also would like to keep the code as it is (as much as possible) to avoid future confusion. The feature that I require is for a random +, - or X question to appear on a panel and the input box to be the method of answering it. I need the question to appear when colliding with something I have called "Blockade".
I have created a collisions script and a script for the maths questions. I have had to call on the collisions script in the maths questions for the Blockade to be destroyed after the question is answered correctly. This is not causing any issues (supposedly). The issue I have found is with this line in the collisions script:
public MathsQuestions2 mathsquestions2 = new MathsQuestions2();
Now I have successfully linked other scripts using this format with no issues. Below I will send the full script for the Collisions and the Maths Questions.
Collisions:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Collisions : MonoBehaviour
{
public GM gm = new GM();
public MathsQuestions2 mq = new MathsQuestions2();
public GameObject Blockade;
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "End")
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.buildIndex + 1);
}
if (col.gameObject.name == "Blockade")
{
mq.QuestionPanel.SetActive(true);
mq.QuestionMaker();
}
//health
if (col.gameObject.name == "Health")
{
Destroy(col.gameObject);
gm.AddLife(1);
}
//points
if (col.gameObject.name == "Gem")
{
Destroy(col.gameObject);
gm.AddScore(100);
//Add 100 Points
}
}
}
The points and health parts have had no issues and only the public MathsQuestion2 line has caused this crashing.
MathsQuestions2:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MathsQuestions2 : MonoBehaviour
{
public Collisions Col = new Collisions();
public int N1;
public int N2;
public int AnswerCorrect;
public Text inputfield;
public Text QuestionBox;
public GameObject QuestionPanel;
string opperand;
public void QuestionMaker()
{
string myOperator = getRandomOperator();
QuestionBox.text = "What is" + N1 + myOperator + N2 + "?";
}
public string getRandomOperator()
{
int randomOperator = Random.Range(0, 3);
N1 = Random.Range(1, 10);
N2 = Random.Range(1, 10);
switch (randomOperator)
{
case (0):
opperand = "-";
AnswerCorrect = N1 - N2;
break;
case (1):
opperand = "+";
AnswerCorrect = N1 + N2;
break;
case (2):
opperand = "*";
AnswerCorrect = N1 * N2;
break;
}
return opperand;
}
public void CheckAns()
{
if (inputfield.text == AnswerCorrect.ToString())
{
Destroy(Col.Blockade);
QuestionPanel.SetActive(false);
}
else
{
QuestionMaker();
}
}
}
The script above has worked with another person though as mine has been called in another script, issues occurred.
To note, when I comment out the public MathsQuestion2 line in the collisions script (as well as the two mq things that call from the MathsQuestions2 script), my project will load. Otherwise, I can't even open the project.
I would appreciate any help that you lot can provide.
Thank you.
You shouldn't instantiate classes that inherit from $$anonymous$$onoBehaviour. It also look like you are creating an infinite loop.
Do you have the scripts on the same gameobject or are they seperate? If you need reference to another class either link it directly in the inspector, or use Getcomponent<> if they are on the same gameObject
I haven't even been able to open the project just because of that script. The collisions script is attached to my character so far. I have not used the Getcomponent thing yet. How would I style it and what would I delete?
Your answer
![](https://koobas.hobune.stream/wayback/20220612100401im_/https://answers.unity.com/themes/thub/images/avi.jpg)