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 SoraMahiro · Dec 12, 2016 at 05:03 PM · c#parentchildrentetris

Unsolved Child transform position problem

I have a parent object that has to stop depending on the position of its children. I need the first and only children to stop at say, -10y. However, with the example I will provide, this is not happening. Instead it seems all children have to be below this point.alt text

The two primary scripts use are as follows:

 public class Tetro:MonoBehaviour
 {
     Instantiator instance;
 
     void Start ()
     {
         instance = FindObjectOfType<Instantiator>();
         StartCoroutine(Fall());
     }
     IEnumerator Fall()
     {
         foreach(Transform mino in transform)
         {
             while(mino.position.y > -10)
             {
                 yield return new WaitForSeconds(1);
                 transform.position -= new Vector3(0, 1);
             }
         }
         StopCoroutine(Fall());
     }
     public void CheckInput()
     {
         if (transform.position.y > -10)
         {
             if (Input.GetKeyDown(KeyCode.RightArrow))
             {
                 transform.position += new Vector3(1, 0);
                 if (!checkEachChildPosition())
                 {
                     transform.position += new Vector3(-1, 0);
                 }
             }
             else if (Input.GetKeyDown(KeyCode.LeftArrow))
             {
                 transform.position += new Vector3(-1, 0);
                 if (!checkEachChildPosition())
                 {
                     transform.position += new Vector3(1, 0);
                 }
             }
             else if (Input.GetKeyDown(KeyCode.UpArrow))
             {
                 transform.Rotate(0, 0, 90);
             }
             else if (Input.GetKeyDown(KeyCode.DownArrow))
             {
                 transform.position -= new Vector3(0, 1);
                 if (!checkEachChildPosition())
                 {
                     transform.position += new Vector3(0, 1);
                 }
             }
         }
     }
     bool checkEachChildPosition()
     {
         foreach (Transform mino in transform)
         {
             Vector2 pos = FindObjectOfType<GameManager.GameManager>().RoundOffPositions(mino.position);
             if (FindObjectOfType<GameManager.GameManager>().CheckIsInGrid(pos) == false)
             {
                 return false;
             }
         }
         return true;
     }
     void AllowNext()
     {
         if (transform.position.y == -10)
         {
             instance.Spawn();
             enabled = false;
         }
     }
     void Update ()
     {
         CheckInput();
         AllowNext();
     }
 }
 

And:

 namespace GameManager
 {
     public class GameManager:MonoBehaviour
     {
         Instantiator instance;
 
         void Start()
         {
             instance = FindObjectOfType<Instantiator>();
             instance.Spawn();
         }
         public bool CheckIsInGrid(Vector2 pos)
         {
             return (pos.x >= -7 && pos.x <= 7 && pos.y > -10 && pos.y < 12);
         }
         public Vector2 RoundOffPositions(Vector2 pos)
         {
             return new Vector2(Mathf.Round(pos.x), Mathf.Round(pos.y));
         }
         void Update()
         {
             
         }
     }
 }

I assume the issue is occurring in Fall() but it shouldn't. Anyone know why this is happening? Or how this can be fixed?

unity.png (12.8 kB)
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 UnityCoach · Dec 13, 2016 at 09:02 PM 0
Share

@Sora$$anonymous$$ahiro, there must be some kind of problem with your web connection or something, you keep posting questions twice. Anyway, I'll have a look at it.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Dec 13, 2016 at 09:03 PM

Disabling a component doesn't stop its Coroutines. It will simply stop receiving Update calls. You must stop the Coroutine when you disable the component I believe.

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 SoraMahiro · Dec 14, 2016 at 06:08 PM 0
Share

I stop the coroutine at the end of the coroutine, so I'm not sure if that is the issue.

avatar image UnityCoach SoraMahiro · Dec 15, 2016 at 09:58 AM 0
Share

I don't understand this : foreach(Transform $$anonymous$$o in transform)

Do you have more than one transform ?

avatar image SoraMahiro UnityCoach · Dec 15, 2016 at 03:59 PM 0
Share

I have a parent object that holds all the children. This iterates over all the children allowing me to check each one in a single loop.

avatar image
0

Answer by SoraMahiro · Dec 16, 2016 at 05:29 PM

This isn't a full answer, but I found that the problem originates in CheckIsInGrid() the problem that seems to be happening, is that when it is called in checkEachChildPosition() it's not being iterated over ALL transforms in the parent. Why, I don't know yet. I've tried it in a few different projects and received different results.

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 SoraMahiro · Dec 16, 2016 at 08:38 PM 0
Share

So, in one project the function seemed to iterate over one axis and not the other, then in another one it seemed to iterate over all axis' but it wasn't stopping the Fall() routine. I am complete stumped...

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

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

Issue with accessing Text by code/how do i acces a Text being spawned. 0 Answers

Why can't I access the public variables of the children class of TestItem in this syntax? I only have access to TestItem variables 1 Answer

How do you add a child through code 2 Answers

Can’t keep the position 0 Answers

Moving grabbed object OnTriggerStay2D 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