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 Robomaster · Nov 01, 2014 at 03:43 AM · errorobjectcrashing

Unity crashing because of added object

So i have the most recent update of unity and it was running fine. Unity was crashing before for no reason so i deleted it off of my computer. Now its re-installed and actually working, i was able to rebuild my game and re-import my code. When i went to test the simple things it was working but when i started adding the in complex buildings unity just crashes. Its not because the graphical power was to much, its just a few blocks, but the code was a lot more complex. I just found out that it's the code that crashing the program theres also a stackoverflowexception error with it that i can't seem to find the root cause of in the program. Can you help me find out why this code is crashing the program.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ArcheryTowerScript : MonoBehaviour {
     public GameObject Arrow;
     public Transform myTransform;
     public List<Transform> EnemyArray = new List<Transform>();
     public int value = 0;
     public int value1 = 0;
     public Transform Cube;
     ArcheryTowerScript call = new ArcheryTowerScript();
     
     // Use this for initialization
     void Start () {
 
         
         
     }
     
     // Update is called once per frame
     void Update () {
         if(EnemyArray.Count > 0)
         {
             value = 1;
             call.arrowCreation();
             
         }
         if(EnemyArray.Count <= 0)
         {
             value = 0;
         }
         
         
         
     }
     
     IEnumerator OnTriggerEnter(Collider collision)
     {
         yield return new WaitForSeconds(0.1f);
         if(collision.transform.tag == "Enemy")
         {
             EnemyArray.Add(Cube);
             
         }
         
     }
     void OnTriggerExit(Collider collision)
     {
         if(collision.transform.tag == "Enemy")
         {
             
             EnemyArray.RemoveAt(0);
             
         }
     }
     
     public Transform recentEnemy()
     {
         return EnemyArray[0];
     }
     public void setCube(Transform myCube)
     {
         Cube = myCube;
     }
     
     public IEnumerator arrowCreation()
     {
         value1 = 1;
         if(value == 1)
         {
             for(int x = 0; x < 10; x++)
             {
                 Instantiate(Arrow, new Vector3(myTransform.position.x,myTransform.position.y,myTransform.position.z), Quaternion.identity);
                 yield return new WaitForSeconds(1);
                 x = 0;
                 
             }
         }
     }
 }

Comment
Add comment · Show 2
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 AlwaysSunny · Nov 01, 2014 at 07:02 AM 0
Share

Possible corrupted asset? $$anonymous$$ight explain crash-on-import or crash-on-adding-to-scene. What kind of asset? A model? What file type? Can you open it in your 3D editing application?

avatar image Robomaster · Nov 01, 2014 at 11:31 AM 0
Share

I was able to take the object out of the scene by deleting the script out of the project. So im messing around with it now, its the script that is causing unity to crash for some reason. I've deleted the script and rewrote it from scratch but thats not the fixing the problem. There is a error in the console saying "StackOverflowExeception"

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Nov 01, 2014 at 04:12 PM

This script is the problem. In particular this line:

      ArcheryTowerScript call = new ArcheryTowerScript();

You shouldn't create MonoBehaviours through the 'new' operator, but no matter how you create them, this line would not work (inside an ArcheryTowerScript). Each 'new' ArcheryTowerScript object is in turn creating a new ArcheryTowerScript object in an endless chain until you crash the stack.

Comment
Add comment · Show 2 · 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 Robomaster · Nov 02, 2014 at 03:00 PM 0
Share

Okay thanks so im making that object so i can call a method within my own class. Everything i seem to be doing doesn't seem to work and i dont want to make the methods static. How would i do this?

avatar image robertbu · Nov 02, 2014 at 05:53 PM 0
Share

I don't understand why you did what you did, so I'm not sure I understand what you are attempt to do here. But just on the face of it:

  • Any script derived from '$$anonymous$$onoBehaviour' should be attached to a game object. So either this script is dragged and dropped on an existing scene object, attached to a prefab which is Instantiated() or is added at runtime using AddComponent().

  • Coroutines should be executed using StartCoroutine()

- Your current code repeatedly executes 'arrowCreaction' even while it is running. This will stack up coroutines.

At the start and end of 'arrowCreation' set a class instance variable:

  creatingArrows = true;

...and at the end:

  creatingArrows = false;

Then your call code on lines 23 - 28 become:

  if(!creatingArrows && EnemyArray.Count > 0)
      {
          value = 1;
          StartCoroutine(arrowCreation());
      }

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

27 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 avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 0 Answers

NullReferenceException: Object reference not set to an instance of an object Random.OnGUI () (at Assets/Random.js:25) 1 Answer

Concave collider error 0 Answers

How to return true if object is in invisible object? 1 Answer

Script wont work... Keep getting an Object not set to an instance error 3 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