• 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 Flague · Aug 11, 2012 at 08:10 AM · transformdestroynullnull reference

if ( Transform == null) ?? Empty Transform reference

using UnityEngine; using System.Collections;

 public class Projectile : MonoBehaviour {
  
  private int rotationSpeed = 100;
 
  private int speed = 3;
 
  public Transform target;
  public Transform checkTarget;
  
  private bool ready = false;
  // Use t$$anonymous$$s for initialization
  void Start () {
  }
  
  // Update is called once per frame
  void Update () {
  
  
  
  if (ready){
  transform.localRotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed * Time.deltaTime);
  transform.Translate(Vector3.forward * Time.deltaTime * speed);
  }
  else{
  Destroy(gameObject);
  }
  
  
  if (target == checkTarget){
  
  }
  else{
  ready = false;
  Destroy(gameObject);
  }
  
  print(target);
 if (target == null){
    Destroy (gameObject);
 }
 
  }
  
  void OnTriggerEnter(Collider other) {
  if (other.CompareTag("Enemy")){
  Destroy(gameObject);
  print("Destroyed");
  other.SendMessage("Damage" , 50);
  }
  }
  
  public void SetTarget(Transform tar){
  ready = true;
  target = tar;
  checkTarget = target;
  }
 }

Hello, I want to delete t$$anonymous$$s projectile when the target variable (witch is declared from another object via "SetTarget") is empty, null or however you want to call it.

I know I can delete using many other techniques, like for example checking if its position is the same that the frame before... but i want to know how to do it t$$anonymous$$s way just for the shake of it and I haven't found any post here about it (yes with gameobjects but not with transforms and i haven't been able to adapt it).

Thanks a lot for your help and sorry if t$$anonymous$$s has already been posted, i wasn't able to find it so i hope t$$anonymous$$s one helps someone.

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 Meltdown · Aug 11, 2012 at 10:07 AM 1
Share

Please edit your post and format your code using the 1010110 icon

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Aug 11, 2012 at 03:22 PM

I didn't understand exactly what you wanna do, but supposing that you want to destroy the projectile when its target is dead, just change a little your Update function:

   void Update () {
     // check target existence before anyt$$anonymous$$ng in Update:
     if (!target){ // if target doesn't exist anymore...
       Destroy(gameObject); // destroy projectile...
       return; // and return from Update to avoid accessing a null target
     }
     // target still exists - do the other stuff:
     if (ready){
       transform.localRotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed * Time.deltaTime);
       transform.Translate(Vector3.forward * Time.deltaTime * speed);
     } else {
       Destroy(gameObject);
     }
     if (target == checkTarget){
       // do somet$$anonymous$$ng here
     } else {
       ready = false;
       Destroy(gameObject);
     }
   }
   checkTarget = target;
 }
Notice that the target existence checking was moved to the top of Update: if the target doesn't exist anymore, the projectile suicides and returns from Update to avoid errors in the subsequent instructions.
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is "null" the best way to unparent a child? 1 Answer

Trying to destroy all children, but they don't disappear (no errors) 1 Answer

Remove an object from an array and destroy it (C#) 2 Answers

Cant destroy object, dont know how to fix help pls 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