Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 justinkatic · Oct 12, 2017 at 02:15 AM · scripting problemgetcomponentbooltrueoncollision

checking Bool true from another Script?

I've looked around and cant work out why this script isn't working for me looks just like all the suggestions that people give others? so I want to check if the bool is true on another script and if it is then I can take damage. this is what I ended up with any help would be great. :D

Script 1 - BulletScript (placed on my bullet prefab)

public bool armed = false;

void OnCollisionEnter (Collision col)

{ if (col.transform.tag == "Shield")

{ armed = true;

   print ("armed " );

}

Script 2 - Health System (placed on my enemy prefab)

void OnCollisionEnter (Collision col)

{ if (GameObject.Find("PlasmaOrb").GetComponent().armed == true && col.gameObject.tag == "Bullet")

{

GoobaHealth -= damage; }

}

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
1
Best Answer

Answer by jabez · Oct 12, 2017 at 02:34 AM

Either make a static script where you store variables, use getcomponent or add a reference to the script!

in script 2

add

public BulletScript bulletScript ;

to where you declare things in script2/health script.

everything in BulletScript needs to be declared as public so you can access them from another script. make sure to drag the bullet script onto script2.

now you can access everything by calling for it

bulletScript.armed = false;

 if(bulletScript .armed){
 }


tbh if i were you i'd use

https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

& use GetComponent

everything in BulletScript needs to be declared as public so you can access them from another script

https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

when you hit it

so something like

 void OnCollisionEnter(Collision collision)    {
         if (collision.gameObject.GetComponent<BulletScript> ()){
             if (collision.gameObject.GetComponent<BulletScript> ().Armed == true) {
             // do logic
             }
         }
     }

or

  void OnCollisionEnter(Collision collision)
         {
             BulletScript bulletScript  = collision.gameObject.GetComponent<BulletScript> ;
             if(bulletScript){
                 if (bulletScript.Armed == true) {
                 // do logic
                 }
             }
         }

Here is some stuff to read/watch about statics too, might be handy in the future. https://unity3d.com/learn/tutorials/topics/scripting/statics

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 justinkatic · Oct 12, 2017 at 02:59 AM 1
Share

thanks so much worked like a charm :D

avatar image jabez justinkatic · Oct 12, 2017 at 04:06 AM 0
Share

You're most welcome, If you need a hand feel free to message me. I hope your game comes along nicely, Good luck C:

avatar image
0

Answer by gnp89 · Oct 12, 2017 at 02:18 AM

This is not clear at all

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 justinkatic · Oct 12, 2017 at 02:28 AM 0
Share

sorry fixed up should look clearer now not sure why it bunched it all toget in one paragraph

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

126 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

Related Questions

Destroying gameobject after animation 1 Answer

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

Writing a bool to a separate script without scripts name? 3 Answers

Unity GetComponent 1 Answer

Trouble getting a bool from player script for camera script 2 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