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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Nov 09, 2016 at 08:00 PM · c#2dscript.crashingfunction call

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.

Comment
Add comment · Show 6
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 Filhanteraren · Nov 09, 2016 at 09:14 PM 0
Share

You shouldn't instantiate classes that inherit from $$anonymous$$onoBehaviour. It also look like you are creating an infinite loop.

avatar image $$anonymous$$ · Nov 09, 2016 at 09:19 PM 0
Share

What would you suggest I change?

avatar image Filhanteraren $$anonymous$$ · Nov 09, 2016 at 09:26 PM 0
Share

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

avatar image $$anonymous$$ Filhanteraren · Nov 09, 2016 at 09:29 PM 0
Share

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?

Show more comments

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

How to make an enemy 2d chasing player? 1 Answer

How to make an enemy 2d chasing player? 2 Answers

How to have a scoreboard system through multiple objects? 0 Answers

converting degrees per second into speed 1 Answer

How to make a scrip stop a coroutine from another script? 0 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