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 /
avatar image
0
Question by Ian9921 · Jan 06, 2017 at 05:39 AM · enemyvariablesmathdestructionassignment

Values not updating before object destruction.

I'm making a Space Invaders style video game for a game making competition. Part of the competition required the game to have some educational value, so I put a number above each enemy so the player would solve simple math problems by shooting the enemies. When a shot hits an enemy, it is supposed to take its individual value and set another variable equal to it, and then destroy itself. However, it seems that the enemies are not successfully assigning their values to the larger variables. My code for the two scripts concerned in this process goes as follows:

Enemy Destruction Script:

 #pragma strict
 
 public var scoreIncrease = 1;
 
 public var mathNumbInput : float;
 public var OnTopRow = false;
 public var OnLeft = false;
 public var OnRight = false;
 
 var testForLowIncrease : float;
 var testForUpIncrease : float;
 
 function OnTriggerEnter2D (info : Collider2D)
 {
     if (info.name == "Shot")
     {
         if (GameMaster.mathNumber == 0)
         {
             GameMaster.mathNumber = mathNumbInput;
             Debug.Log("Number Updated");
         } else {
             GameMaster.mathNumber2 = mathNumbInput;
             Debug.Log("Number Updated");
         }
         if (OnTopRow == true)
         {
             if (testForLowIncrease == EnemyMovementScript.lowerBound)
             {
                 if (OnLeft == true)
                 {
                     EnemyMovementScript.increaseLowerBound += 1;
                     EnemyMovementScript.increaseLowerBound = EnemyMovementScript.increaseLowerBound + EnemyMovementScript.lowIncreaseBonus;
                     EnemyMovementScript.lowIncreaseBonus = 0;
                 }
             }
             else
             {
                 EnemyMovementScript.lowIncreaseBonus += 1;
             }
             if (testForUpIncrease == EnemyMovementScript.upperBound)
             {
                 if (OnRight == true)
                 {
                     EnemyMovementScript.increaseUpperBound += 1;
                     EnemyMovementScript.increaseUpperBound = EnemyMovementScript.increaseUpperBound + EnemyMovementScript.upIncreaseBonus;
                     EnemyMovementScript.upIncreaseBonus = 0;
                 }
             }
             else
             {
                 EnemyMovementScript.upIncreaseBonus += 1;
             }
         }
         GameMaster.score += scoreIncrease;
         GameMaster.levelAdvance += scoreIncrease;
         Destroy(gameObject);
     }
 }


General game management script:

 #pragma strict
 
 var playerSpawner : Transform;
 var playerSpawner2 : Transform;
 
 static var mathNumber : float;
 static var mathNumber2 : float;
 
 public var test1 : float;
 public var test2 : float;
 
 var mathTotal : float;
 
 static var mathGoal : float;
 
 var level = 1;
 
 var size1 : float = 200;
 var size2 : float = 200;
 
 var yOffset : float = 40;
 
 static var score = 0;
 static var levelAdvance = 0;
 
 var lengthToWait = 100;
 var waitTimer = 0;
 
 var playerLife : Transform;
 var gameOverScreen : Transform;
 
 var playerLives = 4;
 static var playerLifeDown = false;
 function Start ()
 {
     mathGoal = Random.Range(2, 19);
 }
 
 function Update ()
 {
     if (mathGoal == 0)
     {
         mathGoal = Random.Range(2, 19);
     }
     test1 = mathNumber;
     test2 = mathNumber2;
 
         if (mathNumber > 0)
         {
             if (mathNumber2 > 0)
             {
                 mathTotal = mathNumber + mathNumber2;
 
                 if (mathTotal == mathGoal)
                 {
                     Debug.Log("Well Done");
                     Instantiate(playerSpawner, Vector3(2.0f, 0, 0), transform.rotation);
                     Player_Movement_Script.playerSpritesOnScreen += 1;
                     mathNumber = 0;
                     mathNumber2 = 0;
                     mathTotal = 0;
                     mathGoal = Random.Range(2, 19);
                 }
                 else 
                 {
                     mathNumber = 0;
                     mathNumber2 = 0;
                     mathTotal = 0;
                     mathGoal = Random.Range(2, 19);
                 }
             }
         }
     }
     if (playerLifeDown == true)
     {
         if (playerLives > 0)
         {
             EnemyMovementScript.speed = 0;
             Player_Movement_Script.speed = 0;
             Player_Movement_Script.playerCanShoot = false;
             Enemy1Control.enemiesCanShoot = false;
             if (waitTimer < lengthToWait)
             {
                 waitTimer ++;
             }
             else
             {
             Instantiate(playerLife, transform.position, transform.rotation);
             waitTimer = 0;
             EnemyMovementScript.speed = EnemyMovementScript.saveSpeed;
             Player_Movement_Script.speed = 10;
             Player_Movement_Script.playerCanShoot = true;
             Enemy1Control.enemiesCanShoot = true;
             waitTimer = 0;
             playerLifeDown = false;
             playerLives -= 1;
             }
         }
         else
         {    playerLifeDown = false;
             Instantiate(gameOverScreen, transform.position, transform.rotation);
         }
     }
 }
 
 function OnGUI ()
 {
     GUI.Box (new Rect (Screen.width/2-size1/2, yOffset, size1, size2), "Lives: " + playerLives + "        Level: " + level + "        Score: " + score + "        Math Goal: " + mathGoal + "        Current Number: " + mathNumber + " testing " + Player_Movement_Script.playerSpritesOnScreen);
 }


Any advice or suggestions would be appreciated.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by juicyz · Jan 06, 2017 at 06:05 PM

First off, I haven't done JS for a while but a couple of things I see that could lead to problems.

 // You should default these to 0 or in the Start() set them to 0.  They need an initial value
 // Also there are several other values that aren't initialized and that's bad practice.
 // Also why declare them as static var mathNumber : float and not static float mathNumber?
 static var mathNumber : float;
 static var mathNumber2 : float;

Ensure that mathNumbInput is actually a number. You should put some Debug.Log statements in your code to see what is actually happening and what the value of everything is. There a ton of things that could be happening but since I don't know the value of everything, I can't tell you.

Put some print statements in and tell me the values then we can work together and figure out your problem as there are too many hypotheticals right now and a lot of code to read... If you wrote this, you should be able to debug this pretty easily as it seems like a simple problem/

Also the value could be getting updated but maybe your GUI isn't updating. I haven't used OnGUI in a while, I think that's the old way and there is a newer GUI.

Comment
Add comment · Show 1 · 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 Ian9921 · Jan 06, 2017 at 06:55 PM 0
Share

Yeah, I could have sworn that I had set mathNumbInput on every enemy, but apparently not. Sorry.

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

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

Related Questions

Unity value assignment help 0 Answers

Variable nos assigned on inspector, but it has been assigned on inspector ? 0 Answers

I want to assign multiple variables without having to type the class on the side every time. 1 Answer

How do I add something on to a float. 1 Answer

Making an enemy face the Player 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