Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Robomaster · Oct 26, 2014 at 12:31 PM · errorclasscallingselecting

Object Tower not selecting right object.

So i have a object that whenever another object that has the enemy tag enters its box collider its suppose to add that object to its array list. The object then sends the first element of the one that been in the collider the longest to the arrow script giving the arrow a target to shoot. Also whenever a enemy cube enters the collider it sends its transform info to the tower class so it can be added to the array. So whats suppose to happen is a enemy enters the area, and the tower shoots it. But whats happening is the tower is shooting the next target or a completely different target than the one that its suppose to be shooting and im not sure what the problem is.

Here is the TowerClass:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ArcheryTowerScript : MonoBehaviour {
     public GameObject Arrow;
     public Transform myTransform;
     public Transform selectedTarget;
     public Transform[] EnemyArray = new Transform[10];
     public int value = 0;
     public int value1 = 0;
     public Transform Cube;
     // Use this for initialization
     IEnumerator Start () {
         myTransform = selectedTarget;
         for(int x = 0; x < 10; x++)
         {
         Instantiate(Arrow, new Vector3(myTransform.position.x,myTransform.position.y,myTransform.position.z), Quaternion.identity);
             yield return new WaitForSeconds(3);
             x = 0;
         }
     
     }
 
 // Update is called once per frame
 void Update () {
     
     if(EnemyArray[0] == null)
     {
         for(int x = 0; x < EnemyArray.Length; x++)
         {
             EnemyArray[x] = EnemyArray[x+1];
         }
     }
 
 }
 
 void OnTriggerEnter(Collider collision)
 {
     if(collision.transform.tag == "Enemy")
     {
         EnemyArray[value] = Cube;
         value++;
     }
     
 }
 void OnTriggerExit(Collider collision)
 {
     if(collision.transform.tag == "Enemy")
     {
         value--;
         EnemyArray[0] = null;
         for(int x = 0; x < EnemyArray.Length; x++)
         {
             EnemyArray[x] = EnemyArray[x+1];
         }
     }
 }
 
 public Transform recentEnemy()
 {
     return EnemyArray[0];
 }
 public void setCube(Transform myCube)
 {
     Cube = myCube;
 }

}

Heres the Arrow Class:

 void Update () {
          ArcheryTower = GameObject.Find("ArcheryTower");
         ArcheryTowerScript at = new ArcheryTowerScript();
         at = ArcheryTower.GetComponent<ArcheryTowerScript>();
         selectedTarget1 = at.recentEnemy();
         Vector3 dir = selectedTarget1.position - myGameObject.position;
         
         myGameObject.rotation = Quaternion.Slerp(myGameObject.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);
             myGameObject.position += myGameObject.forward * moveSpeed * Time.deltaTime;
     
     }
     
     void OnTriggerEnter(Collider collision)
     {
         if((collision.gameObject.name == "MiniUnit(Clone)")||(collision.gameObject.name == "MediumUnit(Clone)")||(collision.gameObject.name =="LargeUnit(Clone)"))
         {
             Destroy(Arrow);
         }
     }
     
     
     
 }

And Heres the cube class:

 void OnTriggerEnter(Collider collision)
 {
     ArcheryTower = GameObject.Find("ArcheryTower");
     ArcheryTowerScript at = new ArcheryTowerScript();
     at = ArcheryTower.GetComponent<ArcheryTowerScript>();
     at.setCube(Cube);
      value = 1;
     if(collision.gameObject.name == "UltraCube")
     {
         orientation = 1;
     }
     if(collision.gameObject.name == "Arrow(Clone)")
     {
         AdjustCurrentHealth(-100);
     }
 }
 
 IEnumerator OnTriggerExit(Collider collision)
 {
     if(collision.gameObject.name == "UltraCube")
     {
         yield return new WaitForSeconds(1);
         orientation = 0;
     }
 }
 
 public void AdjustCurrentHealth(int adj)
 {
     health += adj;
     
     if(health < 0)
     {
         health = 0;
     }
     
     if(health == 0)
     {
         Destroy(gameObject);
     }
 }











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 Deadcow_ · Nov 02, 2014 at 05:59 PM 0
Share

Do you managed to solve this problem eventually?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Deadcow_ · Oct 26, 2014 at 12:44 PM

Why you're using arrays for this? I think List will be much better. You can just simply .Add() new enemy, .Remove() enemy if it dead or comes to OnTriggerExit and get .First() and .Last() enemy whenever you want without addotional calculations and variables

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

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

28 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

Related Questions

Unexcpected Token error with transform.find 2 Answers

WARNING : Assignment to temporary. 1 Answer

public float being defined as object? 2 Answers

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

Using method from main class in custom class 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