• 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 WaterMinecraft · Oct 05, 2015 at 02:16 PM · scripting problemcrashing

Unity Crashing When Attaching Script

So I've been struggling with this bug for the past few days and cannot seem to understand what is happening... I will post my code and explain what is happening:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class TestIns : MonoBehaviour {
 
     public NanoByte nanoByte = new NanoByte();
     public Animator nanoAnim;
     public static int LENGTH = 7;
     public static Text[] children = new Text[LENGTH];
     
     // Use this for initialization
     void Awake () {
         
         children = GameObject.FindGameObjectWithTag("Display").GetComponentsInChildren<Text>();
         Debug.Log(children.Length);
         
         if(children.Length == 5)
         {
 
             children[0].GetComponent<Text>().text = nanoByte.attackValues.attackGS.getSpecial4Attack().ToString();
             children[1].GetComponent<Text>().text = nanoByte.attackValues.attackGS.getSpecial3Attack().ToString();
             children[2].GetComponent<Text>().text = nanoByte.attackValues.attackGS.getSpecial2Attack().ToString();
             children[3].GetComponent<Text>().text = nanoByte.attackValues.attackGS.getSpecial1Attack().ToString();
             children[4].GetComponent<Text>().text = nanoByte.attackValues.attackGS.getStandardAttack().ToString();
             children[5].GetComponent<Text>().text = nanoByte.attackValues.attackGS.getStandardAttack().ToString();
             children[6].GetComponent<Text>().text = nanoByte.attackValues.attackGS.getStandardAttack().ToString();
 
         }
         
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         
         Vector3 move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
         transform.position += move * nanoByte.getSpeed() * Time.deltaTime;
         nanoAnim.SetFloat ("Speed", Mathf.Abs(move.x + move.y));
         
     }
     
     //(COMPLETED) Use parameter to get which button was clicked and pass that in as a parameter so we can
     // just have one method instead of all of these
     public void clickedALevelChanger(Button button)
     {
         Debug.Log(button.name.Split('-')[1]);
         
         nanoByte.attackValues.attackGS.setAttackBasedOnString(button.name.Split('-')[1], int.Parse(button.name.Split('-')[0]));
         
     }
     
 }
 

This code when I drag it over will cause Unity to crash. If I comment out "public NanoByte nanoByte = new NanoByte();" it works just fine. So I think it's something within this class:

 using UnityEngine;
 using System.Collections;
 [System.Serializable]
 public class NanoByte : Character {
         
     public int experiencePoints;
     public AttackValues attackValues;
 
     public NanoByte()
     {
         attackValues = new AttackValues(10, 0, 0, 0, 0);
         base.setHealth(100);
         base.setSpeed(5.0f);
         experiencePoints = 0;
     }
     public NanoByte(int aHealth, float aSpeed, int anAttack, int expPts) : base(aHealth, aSpeed)
     {
         attackValues = new AttackValues();
         attackValues.attackGS.setStandardAttack(anAttack);
         this.setExperience(expPts);    
     }
 
     public void setExperience(int expPts)
     {
         experiencePoints = expPts;
     }
     public int getExperience()
     {
         return experiencePoints;
     }
 
     public void levelUpHealth(int aHealth)
     {
         setHealth(aHealth);
     }
     public void levelUpAttack(int anAttack)
     {
         attackValues.attackGS.setStandardAttack(anAttack);
     }
 
     public void levelUpSpeed(float aSpeed)
     {
         setSpeed(aSpeed);
     }
 }


I believed it was because my classes that were serializable were possibly too long... but I am not sure. If you need the rest of my code I can post it.

 using UnityEngine;
 using System.Collections;
 
 public class Character {
 
     public int health;
     public float speed;
 
     public Character()
     {
         health = 100;
         speed = 5.0f;
     }
     public Character(int aHealthVal, float aSpeedVal)
     {
         setHealth(aHealthVal);
         setSpeed(aSpeedVal);
     }
 
     public void setHealth(int aHealthVal)
     {
         health = aHealthVal;
     }
     public void setSpeed(float aSpeedVal)
     {
         speed = aSpeedVal;
     }
  
     public int getHealth()
     {
         return health;
     }
     public float getSpeed()
     {
         return speed;
     }
 }

Comment
Add comment · Show 9
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 meat5000 ♦ · Oct 05, 2015 at 03:53 PM 0
Share

I dont see the function nanoByte.getSpeed() in the nano script. What happens if you declare nanoByte in the class but initialise it in Start()? Same crash?

Also is it a crash? or freeze, or hang? Describe.

avatar image WaterMinecraft meat5000 ♦ · Oct 05, 2015 at 04:39 PM 0
Share

getSpeed() is in the Character class that it inherits from. And I originally had it in the Awake() function, but I can try the Start().

It is a crash. I drag my script over to the GO in the inspector, and now it's not until I click on the GO that contains it that it crashes.

avatar image meat5000 ♦ WaterMinecraft · Oct 05, 2015 at 04:54 PM 0
Share

Ok Im not much of a C# junkie, but as getSpeed does not appear in the derived class, shouldnt you access it with 'base'?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Unity crashes when compiling this script. Where is the problem? 0 Answers

unity keeps crashing when importing assets and when writing scripts 0 Answers

script causes unity to crash 0 Answers

BCE0164 and self recurring problems 0 Answers

Dodgeroll script crashing game 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