• 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 F_l_o · Sep 26, 2016 at 05:45 PM · scripting problemscript.scripting beginner

Scripting - need help to make my script shorter

Hi, I'm not very good in scripting, i've never made c# before using Unity. So i try to do my best but sometimes, it's not very compact and light.

So i'm here to ask your help about this code, that i would like to make shorter / more compact.

using UnityEngine; using System.Collections;

 public class Coins : MonoBehaviour
 {
       public float cash;
 
 voidUpdate(){
       if(TileManager.score==1)
       {
             if(TileManager.score==3)
             {
                   if(TileManager.score==7)
                   {
                         if(TileManager.score==10)
                         {
                               something like cash = cash + 100
                         }
 
 ETC ... ... ... ...
 
                         else
                         {
                               something like cash = cash + 150
                         }
                   }
                   else
                   {
                        something like cash = cash + 50
                   }
             }
             else
             {
                  something like cash = cash + 30
              }
       }
       else
       {
             something like cash = cash + 10
       }
 

Maybe my code is wrong, i have not test it yet because i don't want to have this huge code if another way, more compact, to make it exist.

Thank you in advance for your help, have a nice day :)

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
1

Answer by Erwin23p · Sep 27, 2016 at 04:12 AM

Hello, I'm learning too, but I'll try to maybe help you. I created the other script too simulating your other script. Well, In the other script I have this:

TileManager.cs

and in the script you're trying to do, I have this:

Cash.cs

I tried, and worked pretty damn well.

I hope I helped you. :D

Comment
Add comment · Show 3 · 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 F_l_o · Sep 27, 2016 at 06:21 PM 0
Share

Hi, thank you for your answer The problem is that the Tile$$anonymous$$anager script that you have done isn't very similar to what i have. $$anonymous$$y script Tile$$anonymous$$anager, add 1 to SCORE when a tile spawn because the player reach the limit of the screen. So in this script, i have just one float which is incremented by 1 when the score grow up.

avatar image F_l_o · Sep 27, 2016 at 06:24 PM 0
Share

But thank you for the script you've done, because it realy helped me in my project :)

avatar image Erwin23p F_l_o · Sep 27, 2016 at 10:37 PM 0
Share

Hi, I'm glad that It helped you. I need a lot of practice to learn C# and Unity programming/scripting so I'm always glad to try to help.

And sorry, I forgot to add some serious explanations: The Tile$$anonymous$$anager is to simulate a way of getting points... so, the important stuff are the boolean, the floats and the GetScore method. all in the update method is to test if it's working. But do you understand what I've done? Or at least, now?

If you want, you can show me your Tile manager script to see if I can help.

So, I'm gonna try to help you a bit more. The cash should grow with every tile spawned, so the cash should go 10 for 1 of score right? I did some modifications, what about now? You just have to call the GetScore method on every tile spawned, and It will add 1 of score and 10 of cash.

Tile$$anonymous$$anager.cs

Cash.cs

I tried, and worked fine.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

88 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 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

Flashlight flickering script? 0 Answers

How do i make my enemies face the player? 2D 0 Answers

Object reference not set to instance for my timer and my text 0 Answers

1st Person shooter, move player forward where cam is facing 1 Answer

Set public gameobject by raycast hit target 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges