• 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 /
This question was closed Oct 13, 2016 at 10:45 PM by DudeBrainz for the following reason:

i fixed it

avatar image
Question by DudeBrainz · Oct 13, 2016 at 06:59 AM · c#2dtriggerenemyfunction

my enemy is broken

so i have an enemy with this code:

 using UnityEngine;
 using System.Collections;
 
 public class enemy : MonoBehaviour {
     [SerializeField]
     private float CrabSpeed;
 
     [SerializeField]
     private GameObject projectile;
     [SerializeField]
     private Collider2D player;
     [SerializeField]
     private LookForShootables lfs;
     [SerializeField]
     private Collider2D radar;
 
     Quaternion zrotate;
     bool lookingright;
 
     bool SeePlayer;
     bool SeePlayerShootable;
     bool Triggered;
 
 
     private float TTN;
     [SerializeField]
     private float MTTN;
     private float reloadtime = 0;
 
     // Use this for initialization
     void Shoot(float mrt)
     {
         if (lookingright == true)
             zrotate = new Quaternion(0, 0, 180, 0);
         if (lookingright == false)
             zrotate = new Quaternion(0, 0, 0, 0);
         Rigidbody bullet;
         bullet = Instantiate(projectile, transform.position, zrotate) as Rigidbody;
         bullet.AddForce(20, 0, 0);
 
         reloadtime = reloadtime + mrt;
     }
 
     void TimeToNeutral()
     {
         if (TTN != 0 && Triggered == true)
             TTN = TTN - 1;
         if (TTN == 0)
         {
             Triggered = false;
             TTN = MTTN;
         }
     }
     void OnTriggerEnter2D(Collider col)
     {
         if (col.tag == "Player")
         {
             Debug.Log("holy crap!");
             SeePlayer = true;
         }
             
     }
 
     void FixedUpdate()
     {
         SeePlayerShootable = lfs.shootables;
         //--------------------------------------
         if (lookingright == !false)
             transform.localScale = new Vector3(-3, 3, 1);
         if (lookingright == !true)
             transform.localScale = new Vector3(3, 3, 1);
         //thesearedebugs
         if (Input.GetKey(KeyCode.H))
             lookingright = true;
         if (Input.GetKey(KeyCode.G))
             lookingright = false;
         if (reloadtime > 0)
             reloadtime = reloadtime - 1;
         //*****************************************************************
         //-----------------------------------------------------------------
         //*****************************************************************
 
         if (SeePlayer == true)
         {
             Triggered = true;
             Debug.Log("triggered!!!");
         }
 
         if (SeePlayerShootable == true && reloadtime == 0)
         {
             Shoot(60);
             Debug.Log("shooting");
         }
         if (SeePlayer == false)
             TimeToNeutral();
     }
 }

and another object has this:

 using UnityEngine;
 using System.Collections;
 
 public class LookForShootables : MonoBehaviour
 {
     [SerializeField]
     private Collider2D player;
 
     public bool shootables;
 
     void OnTriggerEnter2D(Collider col)
     {
         if (col.tag == "Player")
             shootables = true;
     }
 }    
 

the game is in 2d and the problem may lie with the triggers. what am i doing incorrectly? (i've only started coding so i probably did many things incorrectly.)

Comment

People who like this

0 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 doublemax · Oct 13, 2016 at 07:11 AM 0
Share

You need to explain what's broken. What do you do, what do you expect to happen and what happens instead?

avatar image DudeBrainz · Oct 13, 2016 at 10:43 PM 0
Share

i fixed it

1 Reply

  • Sort: 
avatar image

Answer by m1le · Oct 13, 2016 at 10:55 AM

Like doublemax said, explain your problem. And i suggest you to change your if statement : if true if false, by if and else.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Text on Enter Trigger / Text Delay 1 Answer

X number of collisions for destroy 3 Answers

Boss Health Bar 1 Answer

Collision Detection - Strange issues! Working when it shouldnt, not working when it should! 2 Answers

OnTriggerEnter2D doesn't work and I don't understand why. 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