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
1
Question by UnPluks · Jan 19, 2014 at 02:43 PM · gameobjecttransformdestroy

Change target after destroying it.

Hello guys. I was hoping you guys can help me! I am making a game where minions fight, and I want that after a minion kills another, he changes his target to other minion. However I get this error after destroying the gameObject.

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. I leave you my code so if you guys can please point me in the right direction or tell me what I am doing wrong, I would be very grateful!!! 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... } } Thank you!
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

2 Replies

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

Answer by nesis · Jan 19, 2014 at 02:47 PM

Your Debug.DrawLine() method is referencing the target variable, which might be null at that point. Your target==null if statement should be right at the start of your Update() method :)

Comment
Add comment · Show 1 · 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 nesis · Jan 19, 2014 at 02:50 PM 0
Share

An aside: since you're destroying things you're targeting, I'm presu$$anonymous$$g there'll be a case when there's nothing left to target... so you should check that inside your target==null if statement, and return / disable the script to avoid errors. Your go = GameObject.FindGameObjectWithTag("$$anonymous$$mB") will set go to null if no GameObject with that tag could be found.

avatar image
0

Answer by UnPluks · Jan 19, 2014 at 03:48 PM

Oh my god, that solved everything. I feel so dumb now! Thank you, I really appreciate your help. Happy Coding! :D (I will share my code once I finish the game :))

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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Can't remove instantiated prefab 0 Answers

How to turn object into a particle system and back? 0 Answers

How to destroy a transform's parent object. 1 Answer

Confused about GameObject and Transform 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