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 /
  • Help Room /
avatar image
0
Question by Harald921 · Aug 25, 2015 at 02:50 PM · c#scripting beginnerscriptingbasicsbasicsprogramming-basics

How to deal with repetetive code?

Hello guys!

I have a fairly "simple" question. Often I find myself writing very repetetive code, with tons of "if" statements. I'd just like to know how I am actually supposed to deal with this, and what way is the correct way to do it.

Example on repetetive code:

             if (popUpPosition1.transform.childCount >= 2)
                 popUpPosition1.transform.GetChild(0).transform.SetParent(popUpPosition2.transform, false);
             if (popUpPosition2.transform.childCount >= 2)
                 popUpPosition2.transform.GetChild(0).transform.SetParent(popUpPosition3.transform, false);
             if (popUpPosition3.transform.childCount >= 2)
                 popUpPosition3.transform.GetChild(0).transform.SetParent(popUpPosition4.transform, false);
             if (popUpPosition4.transform.childCount >= 2)
                 popUpPosition4.transform.GetChild(0).transform.SetParent(popUpPosition5.transform, false);
             if (popUpPosition5.transform.childCount >= 2)
                 popUpPosition5.transform.GetChild(0).transform.SetParent(popUpPosition6.transform, false);
             if (popUpPosition6.transform.childCount >= 2)
                 Destroy(popUpPosition6.transform.GetChild(0));

I'd just like to know what to use. A friend of mine told me something about having to learn "objects" but I am not entirely sure what he meant by that.

Thanks in advance!

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 Dawaffle99 · Nov 09, 2016 at 02:58 AM 0
Share

repetitive*

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Aug 25, 2015 at 03:01 PM

   public Transform[] popUpPositions; // Drag all pop up in there

then

 for(int i = 0; i < popUpPositions.Length; i++){
      if(i == popUpPositions.Length - 1){
            Destroy( popUpPositions[i].gameObject);
            continue;
       }
       popUpPositions[i].GetChild(0).SetParent(popUpPositions[i+1], false);
 }


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

Answer by Landern · Aug 25, 2015 at 03:02 PM

Well you have some patterns you can using to optimize your code.

It appears each popUpPosition# does the same logic, it would also seem like each is the same type(GameObject or RigidBody perhaps?).

You can start by putting all your popUpPosition objects into the same bucket or collection.

If you're using the Unity Inspector to set each popUpPosition then exposing a public List type should do

 // Normal Code, Class definition, usings, assuming c#
 [System.Serializable]
 public List<GameObject> popUpPositions;
 
 public void SomeMethod()
 {
   // we want to iterate over positions 1 through 5 and give 6 it's own special logic since it differs from the others in logic.  This is reason for Count - 1
   for(int i = 0; i < popUpPositions.Count - 1; i++)
   {
     if (popUpPosition[i].transform.childCount >= 2)
       popUpPosition[i].transform.GetChild(0).transform.SetParent(popUpPosition[i+1].transform, false);
   }
 
   // Get the index of the last item in the list, the list is zero based, the count is 1 base, removing one from count allows the last index in the collection.
   if(popUpPosition[popUpPosition.Count-1].transform.childCount >= 2)
     Destroy(popUpPosition[popUpPosition.Count - 1].transform.GetChild(0));
 }
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Could someone translate these to c#? 1 Answer

How to make one Character follow behind another character? 0 Answers

Need help in translating javascript code into c#. 1 Answer

How do you continuously rotate an object? 1 Answer

How can i run a script with a single key press? 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