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 lucashahne06 · Apr 19, 2021 at 09:42 AM · simplify

Too much code

Hi, I am trying to make an spawning system where items spawn at one of the options, and where there can never be more than 1 item per option. Only, When I wrote the code of just the first 3 items (with the Tutorial-spawnpoints), the code added up to be 360 lines. What I did was generate an random float for every Item, assign a place and if the place was already taken, it would run the same line of code over all options. I tried just adding the value of the spawnlocation by 1, but visual studio told me that was an unnecessary assignment of a value. in the end I ended up with this gigantic piece of code. Does anyone know how I could make this code more compact? Thanks in advance.

  public Transform Spawnpoint1;
  public Transform Spawnpoint2;
  public Transform Spawnpoint3;
  public Transform Tutorial1;
  public Transform Tutorial2;
  public Transform Tutorial3;
  public Transform Tutorial4;
  public Transform Tutorial5;
  public Transform Tutorial6; 
  public Transform Tiny1;
  public Transform Tiny2;
  public Transform Tiny3;
  public Transform Tiny4;
  public Transform Medium1;
  public Transform Medium2;
  public Transform Medium3;
  public Transform Medium4;
  public Transform Medium5;
  public Transform Medium6;
  public Transform Medium7;
  public Transform Medium8;
  public Transform Medium9;
  public Transform Medium10;
  public Transform Medium11;
  public Transform Dynamite;
  public Transform Cog;
  public Transform Handle;
  public Transform Helmet;
  public Transform EnginePart;
  public Transform Screwdriver;
  public Transform Shovel;
  public Transform CrossbowFrame;
  public Transform AxeHandle;
  public Transform Whrench;
  public Transform Lever;
  public bool TutorialIsUsed1 = false;
  public bool TutorialIsUsed2 = false;
  public bool TutorialIsUsed3 = false;
  public bool TutorialIsUsed4 = false;
  public bool TutorialIsUsed5 = false;
  public bool TutorialIsUsed6 = false;
  void Start()
  {
      float LocationDynamite = Random.Range(1, 4);
      float LocationCog = Random.Range(1, 7);
      float LocationHandle = Random.Range(1, 7);
      float LocationHelmet = Random.Range(1, 7);
      float LocationEnginePart = Random.Range(1, 12);
      float LocationScrewdriver = Random.Range(1, 5);
      float LocationShovel = Random.Range(1, 12);
      float LocationCrossbowFrame = Random.Range(1, 12);
      float LocationAxeHandle = Random.Range(1, 12);
      float LocationWhrench = Random.Range(1, 5);
      float LocationLever = Random.Range(1, 5);
     
      if (LocationDynamite == 1)
      {
          Dynamite.position = Spawnpoint1.position;
      }
      if(LocationDynamite == 2)
      {
          Dynamite.position = Spawnpoint2.position;
      }
      if(LocationDynamite == 3)
      {
          Dynamite.position = Spawnpoint3.position;
      }
      if (LocationCog == 1 )
      {
          Cog.position = Tutorial1.position;
          TutorialIsUsed1 = true;
      }
      if (LocationCog == 2)
      {
          Cog.position = Tutorial2.position;
          TutorialIsUsed2 = true;
      }
      if (LocationCog == 3)
      {
          Cog.position = Tutorial3.position;
          TutorialIsUsed3 = true;
      }
      if (LocationCog == 4)
      {
          Cog.position = Tutorial4.position;
          TutorialIsUsed4 = true;
      }
      if (LocationCog == 5)
      {
          Cog.position = Tutorial5.position;
          TutorialIsUsed5 = true;
      }
      if (LocationCog == 6)
      {
          Cog.position = Tutorial6.position;
          TutorialIsUsed6 = true;
      }
      if (LocationHandle == 1)
      {
          if(TutorialIsUsed1 == false)
          {
              Handle.position = Tutorial1.position;
              TutorialIsUsed1 = true;
          }
          else
          {
              if (TutorialIsUsed2 == false)
              {
                  Handle.position = Tutorial2.position;
                  TutorialIsUsed1 = true;
              }
              else
              {
                  if (TutorialIsUsed3 == false)
                  {
                      Handle.position = Tutorial1.position;
                      TutorialIsUsed3 = true;
                  }
                  else
                  {
                      if (TutorialIsUsed4 == false)
                      {
                          Handle.position = Tutorial1.position;
                          TutorialIsUsed4 = true;
                      }
                      else
                      {
                          if (TutorialIsUsed5 == false)
                          {
                              Handle.position = Tutorial1.position;
                              TutorialIsUsed5 = true;
                          }
                          else
                          {
                              
                                  Handle.position = Tutorial1.position;
                                  TutorialIsUsed6 = true;
                          }
                      }
                  }
              }
          }
      }
      if (LocationHandle == 2)
      {
          if (TutorialIsUsed2 == false)
          {
              Handle.position = Tutorial1.position;
              TutorialIsUsed1 = true;
          }
          else
          {
              if (TutorialIsUsed1 == false)
              {
                  Handle.position = Tutorial2.position;
                  TutorialIsUsed1 = true;
              }
              else
              {
                  if (TutorialIsUsed3 == false)
                  {
                      Handle.position = Tutorial1.position;
                      TutorialIsUsed3 = true;
                  }
                  else
                  {
                      if (TutorialIsUsed4 == false)
                      {
                          Handle.position = Tutorial1.position;
                          TutorialIsUsed4 = true;
                      }
                      else
                      {
                          if (TutorialIsUsed5 == false)
                          {
                              Handle.position = Tutorial1.position;
                              TutorialIsUsed5 = true;
                          }
                          else
                          {
                              Handle.position = Tutorial1.position;
                              TutorialIsUsed6 = true;
                          }
                      }
                  }
              }
          }
      }
      if (LocationHandle == 3)
      {
          if (TutorialIsUsed1 == false)
          {
              Handle.position = Tutorial1.position;
              TutorialIsUsed3 = true;
          }
          else
          {
              if (TutorialIsUsed2 == false)
              {
                  Handle.position = Tutorial2.position;
                  TutorialIsUsed1 = true;
              }
              else
              {
                  if (TutorialIsUsed1 == false)
                  {
                      Handle.position = Tutorial1.position;
                      TutorialIsUsed1 = true;
                  }
                  else
                  {
                      if (TutorialIsUsed4 == false)
                      {
                          Handle.position = Tutorial1.position;
                          TutorialIsUsed4 = true;
                      }
                      else
                      {
                          if (TutorialIsUsed5 == false)
                          {
                              Handle.position = Tutorial1.position;
                              TutorialIsUsed5 = true;
                          }
                          else
                          {
                              Handle.position = Tutorial1.position;
                              TutorialIsUsed6 = true;
                          }
                      }
                  }
              }
          }
      }
      if (LocationHandle == 4)
      {
          if (TutorialIsUsed4 == false)
          {
              Handle.position = Tutorial1.position;
              TutorialIsUsed4 = true;
          }
          else
          {
              if (TutorialIsUsed2 == false)
              {
                  Handle.position = Tutorial2.position;
                  TutorialIsUsed2 = true;
              }
              else
              {
                  if (TutorialIsUsed3 == false)
                  {
                      Handle.position = Tutorial3.position;
                      TutorialIsUsed3 = true;
                  }
                  else
                  {
                      if (TutorialIsUsed1 == false)
                      {
                          Handle.position = Tutorial1.position;
                          TutorialIsUsed1 = true;
                      }
                      else
                      {
                          if (TutorialIsUsed5 == false)
                          {
                              Handle.position = Tutorial5.position;
                              TutorialIsUsed5 = true;
                          }
                          else
                          {
                              Handle.position = Tutorial6.position;
                              TutorialIsUsed6 = true;
                          }
                      }
                  }
              }
          }
      }
      if (LocationHandle == 5)
      {
          if (TutorialIsUsed5 == false)
          {
              Handle.position = Tutorial5.position;
              TutorialIsUsed5 = true;
          }
          else
          {
              if (TutorialIsUsed2 == false)
              {
                  Handle.position = Tutorial2.position;
                  TutorialIsUsed1 = true;
              }
              else
              {
                  if (TutorialIsUsed3 == false)
                  {
                      Handle.position = Tutorial3.position;
                      TutorialIsUsed3 = true;
                  }
                  else
                  {
                      if (TutorialIsUsed4 == false)
                      {
                          Handle.position = Tutorial4.position;
                          TutorialIsUsed4 = true;
                      }
                      else
                      {
                          if (TutorialIsUsed1 == false)
                          {
                              Handle.position = Tutorial1.position;
                              TutorialIsUsed1 = true;
                          }
                          else
                          {
                              Handle.position = Tutorial6.position;
                              TutorialIsUsed6 = true;
                          }
                      }
                  }
              }
          }
      }
      if (LocationHandle == 6)
      {
          if (TutorialIsUsed6 == false)
          {
              Handle.position = Tutorial1.position;
              TutorialIsUsed6 = true;
          }
          else
          {
              if (TutorialIsUsed2 == false)
              {
                  Handle.position = Tutorial2.position;
                  TutorialIsUsed1 = true;
              }
              else
              {
                  if (TutorialIsUsed3 == false)
                  {
                      Handle.position = Tutorial1.position;
                      TutorialIsUsed3 = true;
                  }
                  else
                  {
                      if (TutorialIsUsed4 == false)
                      {
                          Handle.position = Tutorial1.position;
                          TutorialIsUsed4 = true;
                      }
                      else
                      {
                          if (TutorialIsUsed5 == false)
                          {
                              Handle.position = Tutorial1.position;
                              TutorialIsUsed5 = true;
                          }
                          else
                          {
                              Handle.position = Tutorial1.position;
                              TutorialIsUsed1 = true;
                          }
                      }
                  }
              }
          }
      }
      
  }

Comment
Add comment · Show 1
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 $$anonymous$$ · Apr 19, 2021 at 11:57 AM 1
Share

I know I am being mean so sorry but holy s*** you scrip is a dumpster fire and you can fix it using lists or arras and if you don't understand lists or arras watch a video on YouTube you will find what you are looking for.

Edit: if you want to make the script just look good use # region and use code folding in the editor option tab under general

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.

Follow this Question

Answers Answers and Comments

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

How can I simplify this script? 0 Answers

Is there a way to simplify this code? 2 Answers

Mesh simplification for Marching Cubes algorithm? 2 Answers

Is it possible to simplify a 3D looped curve? 0 Answers

Any free mesh simplification tools? 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