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 /
  • Help Room /
avatar image
0
Question by Flowered · Apr 13, 2016 at 03:21 PM · transformarray

Transform[] Array - fill it from another Script

Hello! I got two Scripts: 1 Enemy_mob script, with the Array on it, to search for new enemies on the fixed Update(). My Goal is to fill the enemy Array in the Start () of Script 2:

     public Transform[] monsterArray;
     private PlayerScript opponent;
 private Transform player;

     void Start () {
         player = GameObject.FindWithTag ("Player").transform;
         opponent = player.GetComponent<PlayerScript>();
     }
 
     void Update () {
 
         atktime += Time.deltaTime;
         if (player != null) {
             if (!isDead ()) {
                 if (!inRange ()) {
                     chase ();
                 } else {
                     attack ();
                 }
                 //    transformArray = GameObject.FindGameObjectsWithTag ("Player").Select (go => go.transform).ToArray ();
             } else {
                 dieMethod ();
             }
 
         } else {
             if (!newTarget ())
                 {
                 //player = GetNewTarget ();
                 player = GetClosestEnemy (monsterArray);
                 }
         }
 
     }
 
     Transform GetClosestEnemy(Transform[] enemies)
     {
         Transform bestTarget = null;
         float closestDistanceSqr = Mathf.Infinity;
         Vector3 currentPosition = transform.position;
 
         foreach(Transform potentialTarget in enemies)
             
         {
             Vector3 directionToTarget = potentialTarget.position - currentPosition;
             float dSqrToTarget = directionToTarget.sqrMagnitude;
 
             if(dSqrToTarget < closestDistanceSqr)
             {
                 closestDistanceSqr = dSqrToTarget;
                 bestTarget = potentialTarget;
                 player = bestTarget;
                 opponent = player.GetComponent<PlayerScript>();
 
             }
         }
         return bestTarget;
 
     }
 
     void attack()
     {
         if (atktime >= 3) 
         {
             Debug.Log(monsterArray);
             atktime = 0;
             Debug.Log("Angriff_opponent");
             opponent.getHit(damage);
             //    impacted = true;
         }
     }
 
     bool newTarget()
     {
         if (atktime >= 4) 
         {
 //            monsterArray = GameObject.FindWithTag ("Player").transform;
             //Debug.Log("newTargetBoolTrue");
             return true;
         } else 
         {
             //Debug.Log("FALSE");
             return false;
         }
     }
 
 
 

and Script 2, where I actually want to fill the monsterArray

     private MonsterScript opponent;
     private Transform enemy;
 
 void Start () {
         opponent.monsterArray[] += this.gameObject;         //put this gameobj. in the monsterArray of MonsterScript
         enemy = GameObject.FindWithTag ("Enemy").transform;
         opponent = enemy.GetComponent<MonsterScript>();
     }
 
 

If new Monster spawn, it will check in it's Start() to get put into monsterArray, so it will get a Focus if it is in nearest Range. Untill now the monsterArray is empty

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TBruce · Apr 13, 2016 at 05:43 PM

@Flowered I am assuming that your classes look something like below (because of your remark I changed Update to FixedUpdate)

 public class MonsterScript : MonoBehaviour
 {
     public Transform[] monsterArray;
     private PlayerScript opponent;
     private Transform player;
 
     void Start ()
     {
         player = GameObject.FindWithTag ("Player").transform;
         opponent = player.GetComponent<PlayerScript>();
     }
 
     void FixedUpdate ()
     {
         atktime += Time.deltaTime;
         if (player != null)
         {
             if (!isDead ())
             {
                 if (!inRange ())
                 {
                     chase ();
                 }
                 else
                 {
                     attack ();
                 }
                 //    transformArray = GameObject.FindGameObjectsWithTag ("Player").Select (go => go.transform).ToArray ();
             }
             else
             {
                 dieMethod ();
             }
 
         }
         else
         {
             if (!newTarget ())
             {
                 //player = GetNewTarget ();
                 player = GetClosestEnemy (monsterArray);
             }
         }
     }
 
     Transform GetClosestEnemy(Transform[] enemies)
     {
         Transform bestTarget = null;
         float closestDistanceSqr = Mathf.Infinity;
         Vector3 currentPosition = transform.position;
 
         foreach(Transform potentialTarget in enemies)
 
         {
             Vector3 directionToTarget = potentialTarget.position - currentPosition;
             float dSqrToTarget = directionToTarget.sqrMagnitude;
 
             if(dSqrToTarget < closestDistanceSqr)
             {
                 closestDistanceSqr = dSqrToTarget;
                 bestTarget = potentialTarget;
                 player = bestTarget;
                 opponent = player.GetComponent<PlayerScript>();
             }
         }
         return bestTarget;
     }
 
     void attack()
     {
         if (atktime >= 3) 
         {
             Debug.Log(monsterArray);
             atktime = 0;
             Debug.Log("Angriff_opponent");
             opponent.getHit(damage);
             //    impacted = true;
         }
     }
 
     bool newTarget()
     {
         if (atktime >= 4) 
         {
             //            monsterArray = GameObject.FindWithTag ("Player").transform;
             //Debug.Log("newTargetBoolTrue");
             return true;
         }
         else 
         {
             //Debug.Log("FALSE");
             return false;
         }
     }
 }

and

 public class EnemyMobScript : MonoBehaviour
 {
     private MonsterScript opponent;
     private Transform enemy;
 
     void Start ()
     {
         opponent.monsterArray[] += this.gameObject;         //put this gameobj. in the monsterArray of MonsterScript
         enemy = GameObject.FindWithTag ("Enemy").transform;
         opponent = enemy.GetComponent<MonsterScript>();
     }
 }
 

The first problem is that in EnemyMobScript the opponent variable is never initialized when you try to access it like so

 opponent.monsterArray[] += this.gameObject;         //put this gameobj. in the monsterArray of MonsterScript
 

I am surprised that you are not getting an error. But it should look more like

 public class EnemyMobScript : MonoBehaviour
 {
     private MonsterScript opponent;
     private Transform enemy;
 
     void Start ()
     {
         enemy = GameObject.FindWithTag ("Enemy").transform; // find the first Enemy GameObject
         if ((enemy != null) && (enemy.GetComponent<MonsterScript>() != null))
         {
             opponent = enemy.GetComponent<MonsterScript>();     // get the MonsterScript attached to the Enemy GameObject
             opponent.monsterArray[] += this.gameObject;         // put this gameobj. in the monsterArray of MonsterScript
         }
     }
 }

Unless you only have one Enemy in the game you will most likely always be getting the same Enemy with

 enemy = GameObject.FindWithTag ("Enemy").transform; // find the first Enemy GameObject

which may be OK but you may still need to worry about circular reference.

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

48 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

Related Questions

Drag and Drop. Grid Array to save if something is placed not working. Fix?,Drag and Drop Grid array to save the positions of placed Object not working. Fix? 1 Answer

IndexOutOfRangeException error when getting transform of array object 0 Answers

fill public Transform[] on Awake? 1 Answer

Array problem, I'm novice 1 Answer

Object reference not set to an instance of an object + array of positions 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