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 noodlesdefyyou · Mar 30, 2014 at 12:36 AM · beginnerjava

Rewriting one script messed up another script

I finally got my script rewritten and working, though through testing, I've encountered some weird anomalies. First, I was only editing my Enemy Spawner script. The original way I wrote it was like this: http://pastebin.com/ce5ERhf3, and I managed to rewrite it like this: http://pastebin.com/Pe0VwdrV.

However, here are the issues that have arrised since the rewrite.

In the inspector, Enemy Drone Array Length reads 0. It should read 2. The spawner only spawns Enemy Drone 1, instead of randomly 1 or 2 (I've currently only got 2 enemies in the array). The player, controlled by my Game Manager script, no longer spawns. Nor does the level increase when the number of ships spawned hit's 30. Here is my Manager Script: http://pastebin.com/g6Sq41G3. I never touched the manager script once during my Spawner Script rewrite, and even reset the scripts to make sure they were correct in the inspector. Everything is the same as it was before, apart from the Spawner script rewrite. It's not producing any errors in the console, and I'm not really sure how to go about debugging this to figure out what is going on.

Comment
Add comment · Show 4
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 getyour411 · Mar 30, 2014 at 12:37 AM 0
Share

I didn't go through all that but one thing that looked a little off was the use of Random.Range on an INT which is exclusive in that the highest value will never be selected (unless it equals the lower value). Anyway, with a Random.Range of [1,2] you'll never get 2, with a Random.Range of [1,8] you'll never get 8, etc.

avatar image noodlesdefyyou · Mar 30, 2014 at 12:43 AM 0
Share

So I should change it to a range of 1 through .length + 1? Would that be because the range is technically 0-1 ins$$anonymous$$d of 1-2?

avatar image getyour411 · Mar 30, 2014 at 12:47 AM 0
Share

As I posted that I forgot for a second these are zero-filled, so 0-1 is correct; ignore what I wrote since you did use 0 as your start for Random.Range - sorry for the noise!

avatar image noodlesdefyyou · Mar 30, 2014 at 12:55 AM 0
Share

Well I do know why it's only spawning 1 enemy; its choosing a random number between 0 and 0, which will always be 0. Even though I initialized the length in the start function, for some reason it's not registering the proper array length.

1 Reply

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

Answer by andyisbonza · Mar 30, 2014 at 01:56 AM

Perhaps just try using random.range [0,enemyDroneArray.Length]

It might be assigning the value to the array length variable before it is properly initialised. It's better to dynamically query the array length size anyway... Gives you more flexibility in case you later decide to add to it further in the game.

Comment
Add comment · Show 8 · 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 noodlesdefyyou · Mar 30, 2014 at 02:12 AM 0
Share

Ah ha! That fixed the variable spawn issue. Now to figure out why the player isn't spawning, or is the level increasing after 30 enemies have spawned...

avatar image andyisbonza · Mar 30, 2014 at 04:42 AM 0
Share

Where are you calling ShipCounter? I suspect your issue is that the code is never being executed. Although your ShipCounter code will work, it would be better to just divide by 30 and get the result, ignoring the remainder. You could get it down to 1 line:

return (TotalShips / 30)

This also has the advantage of not screwing things up if you call the function twice in a row. For example, if you called ShipCounter with 30 enemies killed, then you would level up. If you called it again straight away, you would still have 30 enemies killed, so you would level up again. Also, the name of the function is also a little confusing - it's not counting ships, it's checking whether you can level up.

Can you explain more fully the player not spawning issue? Is the player not appearing at all, even at the start of the game?

Also, you mentioned you don't know how to debug. Check out the Debug.Log command - use that to print out various variable values to the console.

avatar image noodlesdefyyou · Mar 30, 2014 at 04:53 AM 0
Share

It's controlled by my Game $$anonymous$$anager script like this:

 function ShipCounter()
 {
         if(TotalShips % 30 == 0)
         {
                 Level++;
         }
        
         return(Level);
 }


As far as the player spawning, when I load the game I've given 2 options: Play Game and Options. Clicking Play Game causes the enemies to start spawning, and is supposed to instantiate the player as well.

I know about debug.log, I'm just not sure how to use it in a case like this, for example.

avatar image andyisbonza · Mar 30, 2014 at 05:10 AM 0
Share

Yes, but where do you actually invoke ShipCounter? Unlike your start, update and onGui functions (which are special functions), the code in it will not execute unless you tell it to execute, so my question was "where are you telling it to execute?". You should be calling it every time an enemy is spawned.

Use Debug.Log inside of ShipCounter like this, for example:

 function ShipCounter()
 {
        Debug.Log("Checking if the player has levelled up")
        if(TotalShips % 30 == 0)
         {
                 Level++;
         }
  
         return(Level);
 }

That's gonna tell you if the code is being executed or not - I suspect it isn't.

avatar image noodlesdefyyou · Mar 30, 2014 at 06:49 AM 0
Share

Ah ha! I re-read my old code and found I skipped this little line somehow: level = $$anonymous$$anagerOf.ShipCounter(); So that is fixed now. Now on to why my player is no longer spawning. I've double checked the code and the inspector, everything seems to be in order.

     if(Player == null && PlayerLives >= 1 && Started == true)
     {
         PlayerLives--;
         Instantiate(PlayerShip, Vector3(0, -4.5, 2), Quaternion.identity);
         Player = GameObject.FindGameObjectWithTag("Player");
     }

This line of code call's the player assu$$anonymous$$g those 3 conditions are met. Inside my if(!Started) function, I have this line:

         if(GUI.Button(Rect(Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 50), "Play Game"))
         {
             Started = true;
             Spawner.GetComponent(Enemy_Spawn_Script).IsOn = true;
         }

which switches Started from false to true. Even shows correctly in the inspector that it is started (with a check mark). I really have no idea how this all happened, when all I changed was the spawner script itself, which is why I am at such a loss. Last I knew the code was working as intended.

Show more comments

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

21 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Do i need to learn C n C++ for unity scripting? 3 Answers

Script to teleport player (Beginner here) 0 Answers

Beginner: For Loop freezing unity 1 Answer

Beginner: Box moving up and down. Loop? 1 Answer


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