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 Lunezo · Aug 13, 2017 at 08:20 AM · scripting problemscripting beginnertriggerstower defensescipting

Help with 2d TowerDefense Game

Hello, I am currently following along with a few Tutorial videos and I am currently working on a Tower Defense game that was included in the Project. I am not entirely sure what the conflict is I think it has something to do with the OnTriggerEnter2d function being called because I can manually have my Enemies go to the next CheckPoint by changing the Target int. below is Screenshots of the game and the code I used. I am pretty new to coding and I have searched for a fix but I am stumped. If you can help that would be great Thank you in advance!

EDIT The Issue I am having is my enemy reaches the first checkpoint but he does not move to the next checkpoint he only stays at the first checkpoint

Scene View & Game View

alt text

Enemy & Checkpoint Inspector

alt text

Enemy Movement Script

 public int target = 0;
 public Transform exitPoint;
 public Transform[] waypoints;
 public float navUpdate;

 private Transform enemy;
 private float navTime = 0;

 // Use this for initialization
 void Start () {
     enemy = GetComponent<Transform>();
 }
 
 // Update is called once per frame
 void Update () {
     if (waypoints != null)
     {
         navTime += Time.deltaTime;
         if (navTime > navUpdate)
         {
             if (target < waypoints.Length)
             {
                 enemy.position = Vector2.MoveTowards(enemy.position, waypoints[target].position, navTime);
             } else
             {
                 enemy.position = Vector2.MoveTowards(enemy.position, exitPoint.position, navTime);
             }
             navTime = 0;
         }
     }
 }


 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "waypoint")
         target += 1;
         else if (other.tag == "Finish")
             GameManager.instance.RemoveEnemyFromScreen();
             Destroy(gameObject);
         
 }

}

GameManager Script

 public static GameManager instance = null;

 public GameObject[] enemies;

 public Transform[] spawnPoint;
 public float spawnTime = 1.5f;
 public int maxEnemiesOnScreen;
 public int totalEnemies;
 public int enemiesPerSpawn;

 private int enemiesOnScreen = 0;

 void Awake()
 {
     if (instance == null)
         instance = this;
         else if (instance != this)
             Destroy(gameObject);
             DontDestroyOnLoad(gameObject);
 }

 // Use this for initialization
 void Start () {
     SpawnEnemy();
     InvokeRepeating("SpawnEnemy", spawnTime, spawnTime);
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 void FixedUpdate()
 {
     
 }

 public void SpawnEnemy()
 {
     if (enemiesPerSpawn > 0 && enemiesOnScreen < totalEnemies)
     {
         for (int i = 0; i < enemiesPerSpawn; i++)
         {
             if (enemiesOnScreen < maxEnemiesOnScreen)
             {
                 int spawnIndex = Random.Range(0, spawnPoint.Length);
                 GameObject newEnemy = Instantiate(enemies[0]) as GameObject;
                 newEnemy.transform.position = spawnPoint[spawnIndex].position;
                 enemiesOnScreen += 1;
             }
         }
     }
 }

 public void RemoveEnemyFromScreen()
 {
     if (enemiesOnScreen > 0)
     {
         enemiesOnScreen -= 1;
     }
 }

}

inspector.jpg (144.9 kB)
scenescreen.jpg (189.7 kB)
Comment
Add comment · 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 Zendist · Aug 13, 2017 at 08:42 AM 0
Share

Not sure if this is the primary issue, but you have a problematic else if on line 38 in Enemy $$anonymous$$ovement Script. The else if only captures the Game$$anonymous$$anager.instance.RemoveEnemyFromScreen(); statement, not the Destroy(gameObject);, so the gameObject is always destroyed.

avatar image Lunezo Zendist · Aug 13, 2017 at 10:13 AM 0
Share

Sorry I just saw this Comment it was hidden for some reason but in the video that is what the guy shows him implementing and his code worked fine. The Else if statement is for the Finish point in the game once the enemy reaches the final trigger it will be destroyed so it does not stay in memory "That is what he says at least in the video" Thank you for the suggestion I was thinking the code was Depreciated or something because many Unity tutorials have old code.

2 Replies

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

Answer by Lunezo · Aug 13, 2017 at 03:37 PM

WOW!!! LMAO, I am a so dumb... I unchecked the "Simulated" checkbox because I thought this had something to do with Simulated Gravity... Everything on my Scripts, and in the inspector was perfectly functional except the Triggers were not working because Simulated was disabled... Found this on another Unity Forum post after Hours of searching hopefully this helps someone

The Source I used to solve my problem https://forum.unity3d.com/threads/ontriggerenter2d-not-working.233398/

Comment
Add comment · 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
0

Answer by as30050273 · Aug 13, 2017 at 08:54 AM

Hey! Looks like you need trigger collider on enemy. OnTriggerEnter2D() works only if GameObject, that own this script has collider with 'Trigger' flag.

Comment
Add comment · Show 8 · 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 Lunezo · Aug 13, 2017 at 09:08 AM 0
Share

I'm sorry I don't fully understand what you mean... I am pretty new to coding but what I think you're suggesting is the Enemy $$anonymous$$ovement script that has the OnTriggerEnter() is attached the checkpoints. I have a 2dBoxCollider on both the Checkpoints & the enemy. On the Enemy, it does not have the Trigger Collider ticked because it has to be affected by projectiles that I plan to add in the future. The Checkpoints, however, do have the Trigger component ticked.

avatar image Zendist · Aug 13, 2017 at 09:42 AM 0
Share

The checkpoints Are trigger colliders. Check the screenshots.

avatar image as30050273 Zendist · Aug 13, 2017 at 09:45 AM 0
Share

No, enemy should have trigger to make ontrigger function work. Look at my 2-nd suggestion.

avatar image as30050273 · Aug 13, 2017 at 09:43 AM 0
Share

Ok, if you need your enemy's collider to not be trigger, i can suggest you add a script to checkpoint object. Like that:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Script : $$anonymous$$onoBehaviour { void OnTriggerEnter2D(Collider2D col){ col.gameObject.GetComponent<Enemy$$anonymous$$ovement>().target ++; } }

And ofc remove OnTriggerEnter2d function from enemy script

EDIT: You should do same thing for finish

avatar image as30050273 as30050273 · Aug 13, 2017 at 09:44 AM 0
Share

Oh, im sorry for that strange, 1-line code. Idk how to fix it

avatar image Lunezo as30050273 · Aug 13, 2017 at 10:06 AM 0
Share

As for your suggestion, I just tried adding it and it still does not work its like the first checkpoint is a vortex and the Enemy can't follow the path after hitting the first checkpoint lol

here is what I added I also used your code without the If Statement and it still did not work and I also removed and replaced the "IS TRIGGER" on both the enemy and checkpoint trying to alternate them and neither worked

 void OnTriggerEnter2D(Collider2D col)
 { 
     if (col.CompareTag("Enemy"))
     {
         col.gameObject.GetComponent<Enemy$$anonymous$$ovement>().target ++;
     } 
 } 
 
avatar image Lunezo as30050273 · Aug 13, 2017 at 10:02 AM 0
Share

I still have not managed to have any luck in making the Enemy move from checkpoint 1-21 like the video I have rewatched it many times checked the code line by line and everything and nothing is different except this code I added for 2 spawns ins$$anonymous$$d of 1

          if (enemiesOnScreen < maxEnemiesOnScreen)
          {
              int spawnIndex = Random.Range(0, spawnPoint.Length);
              GameObject newEnemy = Instantiate(enemies[0]) as GameObject;
              newEnemy.transform.position = spawnPoint[spawnIndex].position;
              enemiesOnScreen += 1;
          }
avatar image Lunezo Lunezo · Aug 13, 2017 at 12:00 PM 0
Share

I am clueless why this Trigger Event is not working... I just tried using the Debug.Log and Print to see if it would show in the console and the OnTriggerEnter2d is acting like it does not exist...

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

117 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

Related Questions

OnTriggerEnter2D crashing editor 0 Answers

How to get a variable from a triggers script. 0 Answers

Problems with Gyroscope VR Car Game 0 Answers

Enabling multiple Monobehaviour Components in a game object 1 Answer

[SOLVED] According to "print" message boolean variable turns true but if statment doesn't do stuff., 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