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 Nianderfall · Nov 12, 2017 at 05:04 AM · c#scripting problemenemyspawning-enemies

Help with getting a bool from another script

Okay so I am trying to make a script reference, however the game object the script is attached to is being spawned by a wave spawner using the prefab of it. The game object that I have the other script under is attached to a game object which is attached to the game object carrying the script I am trying to reference to. There are many enemies being spawned and I need my script to only deal with the one it is under. This is my current script and I am trying to check for the bool "dead" from the other script.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class TurnOffLight : MonoBehaviour {

 public GameObject EYE;
   


 // Use this for initialization
 void Start () {

 }
 
 // Update is called once per frame
 void Update () {

     if (GameObject.Find("ENEMY").GetComponent<Enemy>().dead)
     {
         Destroy(EYE);
        
     }
 }

} Any help would be appreciated, I know my description at the top is a bit fuzzy it is kinda hard to explain if you have any questions about what I mean please ask.

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
0

Answer by smkmth · Nov 12, 2017 at 11:34 AM

I am not sure what you mean by 'I need my script to only deal with the one it is under' - so finding it a little difficult to help you. If i were trying to access one or a number of prefabs which have spawned in a scene i would use the GameObject.FindGameObjectsWithTag - the code example here might be relevant? i could use more details if you still need help ?https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

Comment
Add comment · Show 2 · 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 Nianderfall · Nov 12, 2017 at 07:27 PM 0
Share

@smkmth So when I mean the one it is under I have many enemies that are all the same prefab being spawned, each enemy has 2 game objects under them the body and the glowing eye I am trying to make it so when you shoot it the eye object disappears but the script with the "dead" boolean is under is the body not the eye. When you shoot an enemy it will return the value "dead" as true but I want the eye to have an if statement that will remove the eye if "dead" is true. I don't however want to use a tag because all the enemies have the same tag and I don't want when you kill one all the enemies' eyes disappear. Hopefully this makes more

avatar image Nianderfall · Nov 12, 2017 at 07:29 PM 0
Share

sense. forgot to finish that sentence.

avatar image
0

Answer by SnailGirl · Nov 12, 2017 at 12:05 PM

Not completely sure what you want to do, but I think you want to check if the previously instanciated object is "dead" right? Perhaps it would be easier to have an index of the objects, if it is always the previously created Gameobject you want to reach (as in "the one it is under"?).

Make a list and add all your instanciated objects to it. Keep the list in a static script, like a gameControl script or something

(for the control script)

 Public List  enemies = new List();

(for the enemy script)

 int PrevEnemyCount;
 
 void Start()
 {
     enemies.Add(gameobject)
     PrevEnemyCount = enemies.Count -1;
 } 
 
 void Update()
 {
     if (enemies[PrevEnemyCount].GetComponent().dead)
     {
         // whatever needs to be done
     }
 }

I'm not sure this is the best way to do this as I never tested this out, but I think it's better to think about it like this anyway :) good luck!

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 Nianderfall · Nov 12, 2017 at 07:30 PM 0
Share

@BlackSilkStockings What I am trying to do is I have many enemies that are all the same prefab being spawned, each enemy has 2 game objects under them the body and the glowing eye I am trying to make it so when you shoot it the eye object disappears but the script with the "dead" boolean is under is the body not the eye. When you shoot an enemy it will return the value "dead" as true but I want the eye to have an if statement that will remove the eye if "dead" is true. I don't however want to use a tag because all the enemies have the same tag and I don't want when you kill one all the enemies' eyes disappear.

avatar image SnailGirl Nianderfall · Nov 12, 2017 at 09:05 PM 0
Share

If I understood correctly, the eye is a child of the enemy? in that case you can check the parent. The parent is under the transform (gameobject.transform.parent). Check if the dead is on with something like GetComponentsInParent: https://docs.unity3d.com/ScriptReference/GameObject.GetComponentsInParent.html

I would make an int counter or something, to see how many hits the enemy has gotten, and then set the component from the parent to the children, so you don't have to have the check constantly in the update function. that way what happens to the enemy can affect the children (aka eyes)

so ins$$anonymous$$d of checking a bool, something like this

 //for each hit on the enemy, ++ the hit 
 
 int hit;
 
 //if it has been hit more than twice, you destroy the first child, which is the first eye
 if (hit>2)
 {
         Destroy (transform.GetChild(0).gameObject);
 }
 //if more than 4, destroy the second eye
 if (hit>4)
 {
         Destroy (transform.GetChild(1).gameObject);
 }
 
 
 
 
 
avatar image Nianderfall SnailGirl · Nov 13, 2017 at 11:39 PM 0
Share

This worked! Thanks!

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

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

Related Questions

Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers

I need Help spawning game objects (enemys) from the top of my screen,How can i spawn my Game Objects on top of the sreec randomly 1 Answer

Wave Spawner not working |Does not detect if an object is destroyed 1 Answer

How do I get enemies to only chase me in certain areas 0 Answers

Multiple Cars not working 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