Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 Bill Cosby · Nov 18, 2012 at 01:31 AM · c#getcomponentbooleantriggersif statement

(C#) If statements based on variables in another script

Hello, I have a script called "enemyscript" where an if statement is based on the status of a boolean in another script, but it's not quite working right. I have a script called "movement" that contains the boolean variable called "isAttacking". The idea is that once the player hits the trigger, if the variable is true, I want the object the enemyscript is attatched to to be destroyed, but if the variable is false, I want the current scene to be reloaded. Here is the enemyscript:

 using UnityEngine;
 using System.Collections;
 
 public class enemyscript : MonoBehaviour {
 
     void OnTriggerEnter(Collider other) {        
         if(other.name == "Player"){
         
             if(other.gameObject.GetComponent<movement>().isAttacking == true){
         
                 Destroy(this.gameObject); 
         }
         else
         {
             Application.LoadLevel(Application.loadedLevel);    
         }
         }    
     }
 }

and here is the script containing the boolean I'm trying to check (only the top part is relevant to this, but I'm posting the whole thing for the sake of completeness):

 using UnityEngine;
 using System.Collections;
 public class movement : MonoBehaviour {
 
     public float speed = 6.0F;
     public float jumpHeight = 8.0F;
     public float gravity = 20.0F;
     public bool isAttacking = true;
     private Vector3 moveDirection = Vector3.zero;
     CharacterController controller;
     
 
     void Start(){
 controller = GetComponent<CharacterController>();
 }
     void Update() {                    
         if (controller.isGrounded) {
               moveDirection = Vector3.forward;
               moveDirection = transform.TransformDirection(moveDirection);
               moveDirection *= speed;        
               
 
             if (Input.GetButtonDown("Jump"))
             {
                 moveDirection.y = jumpHeight;
             }            
     
         }       
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 }

My problem is that the game object on which the enemyscript is attatched to is being destroyed regardless of the state of the isAttacking variable. Even if I change isAttacking to false in the movement script, it's being destroyed as opposed to reloading the scene. What should I do to fix this?

Comment
Add comment · Show 1
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 whydoidoit · Nov 18, 2012 at 03:46 PM 0
Share

Are you sure its false? It's definitely false in the inspector when the game is running? Just a gotcha that, the value that is the first default you set is the one that is maintained. Otherwise, looks fine and should work.

1 Reply

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

Answer by Meltdown · Nov 18, 2012 at 04:17 PM

Rather access properties in your classes like this... It makes for cleaner code, and always works without the inspector intefering...

 [HideInInspector]
 public bool IsAttacking { get { return _isAttacking; } set { _isAttacking = value; } }
 bool _isAttacking = false;


Then just access the IsAttacking property like...

 if(other.IsAttacking)
    Debug.Log("Is Attacking");

And if you want a variable to be in the inspector, but you don't it to be public, then use the SerializeField attribute..

 [SerializeField]
 bool IsAttacking = false;
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 Bill Cosby · Nov 18, 2012 at 06:03 PM 1
Share

That did the trick and everything works perfectly now! Thank you so much for your 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

12 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

Related Questions

If statement breaking button color copy 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Bool not returning correct value from getCompoent 1 Answer

I have a specific problem with my trigger 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