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 Rosscoe3 · Sep 01, 2015 at 03:40 AM · script.getcomponentfindboolcheck

How do I check a bool from another script??

Hi so I've been looking through forms all night.. and haven't found something that will work.. In EnemyScript i am trying to see if a bool from PlayerScript is false:

EnemyScript: using UnityEngine; using System.Collections;

public class EnemyScript : MonoBehaviour { public PlayerScript checkDeath;

 void Start ()
 {
     GameObject g = GameObject.FindGameObjectWithTag ("Player");
     checkDeath = g.GetComponent<PlayerScript> ();
 }
 void OnMouseDown ()
 { 
     if (health <=0)
     //if the health is 0 or less.. 
     {
         if(checkDeath.Dead = false)
         //and if the object isn't active..
         {
             Destroy(gameObject); 
         }
     }
      
 }

I took out the unimportant stuff.. but i'm trying to find out if the bool "Dead" from PlayerScript is false.. and if it is then to destroy the object..

Im not sure what you need to see from PlayerScript other than the fact that there is a public bool on it.. Please help!! thanks!!

Comment
Add comment · Show 3
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 Mikilo · Sep 01, 2015 at 03:56 AM 0
Share

Hello!

You have:

GameObject g = GameObject.FindGameObjectWithTag ("Player");

Which will look for the first GameObject with tag Player.

Thus all your EnemyScrip will use the same reference. Is this intended?

avatar image itsharshdeep · Sep 01, 2015 at 04:08 AM 0
Share

Hello @$$anonymous$$ikilo

I just missed your comment while posting the answer. I'll delete my answer but after one clarification . Is @Rosscoe3 's if condition is not wrong ? You can check my answer and tell me. I don't want to give wrong suggestion to anyone. So please verify. & also I'm assu$$anonymous$$g there is only one player in the whole game.

avatar image Mikilo · Sep 01, 2015 at 05:09 AM 0
Share

@itsharshdeep You also true. I confirm!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by itsharshdeep · Sep 01, 2015 at 04:00 AM

From your code what I understand is your check condition is assigning the value instead of checking, you need to place "==" instead of "=" in 'if' condition.Try to replace the code

          if(checkDeath.Dead = false)
          //and if the object isn't active..
          {
              Destroy(gameObject); 
          }
 
 To 
 
          if(checkDeath.Dead == false)  //  or can also use  if(!checkDeath.Dead)
          //and if the object isn't active..
          {
              Destroy(gameObject); 
          }

Or second problem you may facing is the Player is dead and he is not more present in the scene so the script is also get destroyed , but you are trying to access that but it is giving you the error.

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 Rosscoe3 · Sep 02, 2015 at 02:29 AM 0
Share

Thanks for the comment @itsharshdeep !! hmm.. well it is not working sadly :( The player himself is not dead... but the game is not finding the value of the bool.. not sure why this is happening. heres the error:

NullReferenceException: Object reference not set to an instance of an object EnemyScript.On$$anonymous$$ouseDown () (at Assets/Scripts/Enemy Scripts/EnemyScript.cs:50) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)

avatar image Mikilo · Sep 02, 2015 at 02:36 AM 0
Share

It says checkDeath is null. But you are trying to use it.

That also says:

 GameObject g = GameObject.FindGameObjectWithTag ("Player");
 checkDeath = g.GetComponent<PlayerScript> ();

does not find GameObject, or it contains no PlayerScript.

avatar image itsharshdeep · Sep 02, 2015 at 04:33 AM 0
Share

Please search in you hierarchy for 'PlayerScript' & check on which component the script is attached even in Play mode. $$anonymous$$indly keep track on that.

Or I you agree I can help you on TeamViewer ($$anonymous$$ay be I can sought out)

Thanks.

avatar image
0

Answer by Kajal savani · Sep 02, 2015 at 09:39 AM

Declare your dead bool type to any other script (Public Static bool Dead;) that script is not destroy like PlayerScriptis destroy when Player gameobject is destroyed...

and You can access that dead bool variable to playerScript or any script like(othertScrpt. Dead = true;)....

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 Mikilo · Sep 02, 2015 at 10:18 AM 0
Share

Seriously... Do not give advice like that... It is like, you dude want an ice cream? Go to the factory, get some stuff, buy a fridge, prepare your meal and come back in 12 hours. Ins$$anonymous$$d of simply buying one at the shop nearby...

Using a static for that kind of problem is neither relevant, nor smart.

At the end of your career of developer, you will only use static and keep such bad behaviours like cockroach.

No no no and no, do not you use static. It is overkill.

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

31 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

Related Questions

Can't Call function in another script 1 Answer

How do I change a clicked on GameObjects variable when a boolean (Next Turn) is true? 1 Answer

Get bool value from one script to another (C#) 2 Answers

GetComponent, set boolean to true but it willn't revert 3 Answers

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 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