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 /
avatar image
0
Question by willaay96 · Jan 03, 2018 at 03:07 AM · c#platformer

Moving an Inactive gameobject after period of time

Hello, I've started trying to replicate the game cuphead, I'm trying to make a boss level like the 3 Vegetables level. At the moment I have 3 phases, where in each phase the boss does a different type of attack. Now I want to have the boss move to a new position when its time for the new phase. In the cuphead level, how I imagined it happening is: Damage boss, Move on to next stage, boss turns inactive (due to it being invisible and colliders disappearing etc), then after amount of time, boss is active again. So I wrote my code like such :

IEnumerator Move(Vector3 position, float time = 0f) { gameObject.SetActive(false); if (transform.position != position) { transform.position = position; } yield return new WaitForSeconds(time); gameObject.SetActive(true); }

And it gets called in the void Update() function like so: if (healthPercent <= 1f) { if (!attack1Started) { attack1Started = true; StartCoroutine(Move(positions[0], 0)); StartCoroutine(Attack1()); } }

However I get an error saying "Coroutine couldnt be started because the gameobject is inactive". What would be the correct way of doing this? Would it be better to remove the sprite and disable the colliders so its similarly invisible? Or better way of scripting? For reference here is the boss fight I'm taking about https://www.youtube.com/watch?v=Mps2dyypfGw

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

2 Replies

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

Answer by Lysander · Jan 03, 2018 at 08:54 PM

GameObjects that are inactive can be referenced just fine, and any inactive Component, or Component on an inactive GameObject can have functions run on them without any problems. Being made inactive only does a few things: fire OnDisable on every disabled script, stop all coroutines and invokes that are currently running on those scripts (and prevent you from starting more, as you've now seen), the update cycle functions on each script will stop firing (so like Update/LateUpdate/FixedUpdate will all stop), and scene-searching functions like GameObject.Find will no longer be able to locate that object by name. Specific Components also have their own internal OnDisable and OnEnable-like functions that you can't see, so Colliders and Rigidbodies will remove themselves from the Physics system temporarily, MeshRenderers will stop rendering the mesh, Animators will stop animating the avatar, etc, but they can still be accessed and data pulled from them and set to them just fine.

Being disabled definitely doesn't make any existing references to the GameObject or Components attached to it null- only actually destroying the GameObjects or Components will do that, so anything that previously obtained a reference will still hold that reference, and you can still use GetComponent / GetComponentInParent / GetComponentInChildren, etc, just fine (for the Children versions though, you have to set the optional second parameter "includeInactive" to true). As an added note: all disabled Components will also still receive collision and trigger events, if the GameObject they're attached to is active and has an active collider/trigger, so that they can re-enable themselves if needed.

As to your specific problem, I would make a manager that controls the state of the boss (and other enemies, probably). It can be a parent of the boss, or it can be a completely different GameObject, or it can just be a static manager and not a GameObject at all, but either way it should be the one that holds the boss's state, and it should be the one that re-enables it after X seconds. It depends on whether you only want the boss to be invisible, but still acting, or whether it's not going to be doing anything at all for those seconds. One option might be to have one boss object that's the manager, and multiple children with logic that function as separate states of that boss (one or more may have the big visual sprite representing it). This makes it so you can do things like have objects being thrown in from the sides of the level during some boss stages, or dropping from the sky, etc, but disable that set of behaviours as needed, while enabling others..

In the end, it's really up to you- there are dozens of ways of handling this, and each has their advantages and disadvantages. Whatever seems easiest / most logical for what you need should be your approach.

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 willaay96 · Jan 04, 2018 at 01:43 AM 0
Share

$$anonymous$$uch appreciated, the thing I find quite difficult at the moment, is figuring out different ways of doing things, I'm still just kind of going about it the basic ways I know how, so this response gave me some ideas for other ways of doing those things and am grateful for that!

avatar image
0

Answer by tormentoarmagedoom · Jan 03, 2018 at 01:32 PM

Good day.

When a Gameobject like "Apple" is SetActive(false), all scripts trying to interact with Apple scripts or transforms or components, will get null.

If you will need that object in the future, but need to "disappear" fora time, first, all scripts that will need to interact with apple, needs to find it and store the GameObject before apple gets SetActive(false). this way some things can be changed while Apple is SetActive(false), but depending on what you need to change from apple while is SetActive(false) you will need to wait to SetActive(true) tod o it.

The other option you have is or enable=false all components of the apple like colliders and images, or maybe just move the apple outside the camera view while you need.

Good luck !

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

424 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[ C#] Character Movement based on Camera Direction. 2 Answers

How can I make my player attack enemies without getting errors? 0 Answers

I have a problem with coins 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