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 BartiriX · Dec 12, 2016 at 04:08 PM · c#errorbeginnerparent-child

Can't get object to be reactivated by parent.

Hi everyone, I've been breaking my neck on this problem for a few hours now and since I can't find any info that helps me anywhere apparently, I've come here to ask for some help. I know what the issue is, I think, but I dont know how to work around it or solve it.

So I have an emptyobject (Ill call it weapon) with a trigger on it, and when it collides with an object tagged as "enemy", the weapon's script sets a boolean in the script of the enemy it's killing, "dead", to true. This is just some background.

When dead becomes true, it sets the Enemy-object to inactive. After this a cooldown comes up (supposedly), after which the enemy becomes active again. (As a form of respawning.) Herein lies the problem (I think): The parent cant activate the inactive child, because the code of the child cant be reached BECAUSE the child is inactive... like a vicious cycle?

Please, if I got this all wrong, do explain it to me! Im really eager to learn and I've only gotten started like a week ago. Thanks for your time in advance!

The code of the EnemyController, the script on the Enemy itsself:

 public class EnemyController : MonoBehaviour {
     public float knockBackX, knockBackXneg, knockBackY, stunTime;
     public bool dead;
     public bool activated;
     public GameObject enemyPrefab;
     public float currentcooldown, recooldown;
 
     void Start () {
         dead = false;    
         activated = true;
         gameObject.SetActive (true);
     }
 
     void Update () {
         if (dead == true){
             activated = false;
             gameObject.SetActive (false);
         } 
     }
 }

The code of the parent of the "enemy" object:

 public class enemyParent : MonoBehaviour {
 public int currentcountdown;
 
     void Start (){
     }
 
     void FixedUpdate () {
         if ((GetComponentInChildren<EnemyController> ().activated == false) && (currentcountdown != 20)) {
             currentcountdown += 1;
         } else if ((GetComponentInChildren<EnemyController> ().activated == false) && (currentcountdown == 20)) {
             GetComponentInChildren<EnemyController> ().dead = false;
             GetComponentInChildren<EnemyController> ().gameObject.SetActive(true);
         }
     }
 }
 

Oh and last but not least, when I destroy the enemy, it gives me an error that keeps stacking rapidly, up to a few hundred in a few seconds. This is the error:

 NullReferenceException: Object reference not set to an instance of an object
 enemyParent.FixedUpdate () (at Assets/AI/enemyParent.cs:8)
 

Comment
Add comment · Show 2
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 12, 2016 at 05:25 PM 0
Share

When is the enemy being destroyed?

avatar image BartiriX SoraMahiro · Dec 12, 2016 at 05:45 PM 0
Share

Sora$$anonymous$$ahiro Hey, thanks for your reply! The enemy doesn't get Destroy()'ed, I just noticed that was some really poor wording. It just becomes deactivated under certain conditions.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by SoraMahiro · Dec 12, 2016 at 05:54 PM

When the Object become SetActive = false; the object also becomes null, so that answers the null reference exception. However, as for the Parent not being able to set the child to active, i would try using the enabled method instead. It takes a boolean argument as enabled = true or enabled = false . This can be changed by another script while still setting the game object to be inactive or active. Now if you want the object to disappear when it becomes deactivated, you have to options, SetActive which we seem to have a problem with, or you could actually use the Destroy() method as an alternative and instantiate a new one after time has elapsed. Either of these methods, I suggest having a Game Manager that controls game functionality such as Object Activation/Deactivation because this will allow you to have a little more functionality, if you'd like any examples of any of the three methods, just let me know.

Comment
Add comment · Show 6 · 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 BartiriX · Dec 12, 2016 at 06:26 PM 0
Share

Hey there, I indeed want the object to disappear when it becomes deactivated. How would I apply destroy, though? Would I have to make the parent destroy the object? Or would I have to make the parent check if it has a child, and otherwise if it doesnt, instantiate an enemy prefab as new child?

avatar image SoraMahiro · Dec 12, 2016 at 06:52 PM 0
Share

You can create the Game $$anonymous$$anager like I said, then run through the motions, and ins$$anonymous$$d of setting the child to deactive, destroy that whole instance of the enemy, parent and all, then use the Game $$anonymous$$anager to instantiate a new prefab of the enemy. There will need to be references between all three objects, and also, to keep the null reference exception from showing up, you'll need to do some work to make it so that the function returns when it can't find the object such as:

 if ($$anonymous$$yGameobject != null) {
     //Do something
 }
 else {
     return;
 }
avatar image BartiriX SoraMahiro · Dec 12, 2016 at 07:10 PM 0
Share

Ahh, well, I have no idea how to even begin to make a Game $$anonymous$$anager, so I'll have to research that tomorrow! :)

avatar image SoraMahiro · Dec 12, 2016 at 07:14 PM 0
Share

Oh, that's easy, you just need an empty game object and a script attached to it that's called Game$$anonymous$$anager. It also helps if you name the empty Game$$anonymous$$anager as well. But you should only use this for the functionalities of your game that the other objects can't do themselves. Such as instantiating a brand new prefab.

avatar image BartiriX SoraMahiro · Dec 12, 2016 at 09:18 PM 1
Share

Ahh, alright! I'll be looking into building a respawn function into it then :) I first gave the enemy object a parent with a sort of managing script on it, but a global gamemanager is probably much more transparent to work with. Thanks for your great help so far! :D

avatar image SoraMahiro BartiriX · Dec 12, 2016 at 10:33 PM 0
Share

No problem, always willing to help : )

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

287 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

Related Questions

C# unexpected symbol error 1 Answer

Unity 5 doesn't detect some positions in Vector3 0 Answers

Position Child Object to center of Parent Object [SOLVED] 1 Answer

Character flies up and gets stuck there, won't move forward? 1 Answer

How to convert an inputfield's string to an int? (C#) 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