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 UnPluks · Jan 19, 2014 at 02:39 PM · destroyenemytargettargeting

Changing AI target, after destroying gameObject

Hello. I am working on a game where I have minions fighting. My problem is that when I destroy the first target, I cannot reset a new target for the minion. This is what the console says:

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.

Your script should either check if it is null or you should not destroy the object. Please, if someone could tell me what is wrong with my code I will be very grateful, as the last 7 hours I have been trying to find a solution without success. public class aController : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; public float atackDistance;
private Transform myTransform; private GameObject go;
void Awake(){ myTransform = transform; }
// Use this for initialization void Start () { SetTarget(); }
// Update is called once per frame void Update () { Debug.DrawLine(target.transform.position, myTransform.position, Color.yellow);
//finds target if (target == null) { SetTarget (); }
//Look at target myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed Time.deltaTime);
if ((Vector3.Distance(target.position, transform.position)) > atackDistance)//Move until in range to atack { //Move towards target myTransform.position += myTransform.forward
moveSpeed * Time.deltaTime; } else{ atack (); } }
void SetTarget() { go = GameObject.FindGameObjectWithTag("teamB"); target = go.transform; }
void atack() { //ATACK CODE HERE... } }
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 Invertex · Jan 19, 2014 at 02:47 PM 0
Share

Perhaps wrap an:

 if(target != null)
 {
 }

Around the whole area starting from //Look at target, and end after else{}. ? Perhaps for a frame, there exists no object with $$anonymous$$mB and it falls over on itself.

Another though would be to change

 myTransform = transform;

to

 myTransform = this.gameObject.transform;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 19, 2014 at 04:31 PM

Just because you destroy something doesn't mean the reference to that object becomes null. You have a couple of choices here. A bit hackish but easy solution would be to call SetTarget() in Start() and at the top of Update(). That will make sure that 'target' is a valid transform. If you don't go that route, then you are going to have to somehow inform this code that the target has been destroyed and therefore it should acquire a new target. You could do that by the projectile directly referencing the code (GameObject.Find() and GetComponent()) or by SendMessage(). A bit cleaner (IMHO) but a bit more complex solution would be to use C# events and delegates:

http://answers.unity3d.com/questions/600416/how-do-delegates-and-events-work.html

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

19 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

Related Questions

revise enemy code to player code 0 Answers

My Tower is acting weird (shooting at one enemy, killing different enemy) 0 Answers

Melle system destroy game object not working despite no errors and declining health 1 Answer

Moving Enemies? 1 Answer

Enemy spawn then select target? 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