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 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 this 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 this 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 this 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 this has already been posted, i wasn't able to find it so i hope this 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 anything 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 something 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.

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

instantiate,destroy,gain speed in time 2 Answers

Null Reference exception when calling using "script.gameObject.GetComponent 1 Answer

Cleanly destroying transforms 0 Answers

When Is This Script Destroyed? 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