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 Cbjfan1 · Jul 31, 2013 at 04:56 PM · errorshootingnullaimdestroyed

Detect if Transform is destroyed

I'm making a script for a turret that aims at the object and fires at it when inrange = true, but the gun just keeps firing after the object is destroyed.

AIM SCRIPT

 var target : Transform; //the enemy's target
 var tarpos : Vector3;
 var moveSpeed = 50; //move speed
 var rotationSpeed = 3; //speed of turning
 var thispos : Vector3;
 var inrange = false;
  
 var myTransform : Transform; //current transform data of this enemy
  
 function Awake()
 {
      myTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 { 
      target = GameObject.FindWithTag("EnemyLand").transform; //target the player
  
 }
  
 function Update () {
 if(target == null){
 print("working");
     inrange = false;
     }
 tarpos = target.position;
 thispos = transform.position;
 
 
 if(Vector3.Distance (thispos, tarpos) < 25){
             inrange = true;
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
                 //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
     if(Vector3.Distance (thispos, tarpos) > 25){
     inrange = false;
     }
     }

SHOOT SCRIPT

 var bullet : Transform;
 var reloadTime = 0.2;
 var LTAcube : GameObject;
 var canfire = true;
 var muzzle : Transform;
 var on : boolean;
 
 function Update () {
 on = LTAcube.GetComponent("LTAI").inrange;
 if(on == true){
 Debug.Log("On Is True");
 if(canfire == true){
 Debug.Log("CanFire Is True");
 Fire();
 Debug.Log("Sending Fire");
 
 }
 
 }
 
 }
 
 function Fire () {
 Instantiate(muzzle, transform.position, transform.rotation);
 Instantiate(bullet, transform.position, transform.rotation);
 Reload();
 Debug.Log("Sending Reload");
 canfire = false;
 if(on == false){
 canfire = false;
 }
 }
 
 function Reload () {
 yield WaitForSeconds (reloadTime);
 Fire();
 }
 
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 Lovrenc · Jul 31, 2013 at 05:02 PM 0
Share

You have a target i see. You can get gameObject from transport and therefore any script. So obviously one of the scipts on your target regulates weather it is dead or alive. This script could change the tag or dispatch message or event.

2 Replies

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

Answer by Cbjfan1 · Jul 31, 2013 at 06:18 PM

I got it to work :D

AIM SCRIPT

 var target : Transform; //the enemy's target
 var tarpos : Vector3;
 var moveSpeed = 50; //move speed
 var rotationSpeed = 3; //speed of turning
 var thispos : Vector3;
 var inrange = false;
  
 var myTransform : Transform; //current transform data of this enemy
  
 function Awake()
 {
      myTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 { 
 InvokeRepeating ("Spawn", 1, 1);
  
 }
 
 function Spawn() {
 target = GameObject.FindWithTag("EnemyLand").transform; //target the player
 }
  
 function Update () {
 if(target.GetComponent("health").heal <= 0){
 Debug.Log("DISABLING");
 target = null;
 inrange = false;
 }
 tarpos = target.position;
 thispos = transform.position;
 
 
 if(Vector3.Distance (thispos, tarpos) < 25){
             inrange = true;
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
                 //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
     if(Vector3.Distance (thispos, tarpos) > 25){
     inrange = false;
     }
     }

SHOOT SCRIPT

 var bullet : Transform;
 var reloadTime = 0.2;
 var LTAcube : GameObject;
 var muzzle : Transform;
 var on : boolean;
 
 function Start()
 { 
 InvokeRepeating ("Spawn", 1, 1);
  
 }
 
 function Spawn() {
 on = LTAcube.GetComponent("LTAI").inrange;
 Fire();
 }
 
 
 function Fire () {
 if(on == true){
 Instantiate(muzzle, transform.position, transform.rotation);
 Instantiate(bullet, transform.position, transform.rotation);
 Reload();
 Debug.Log("Sending Reload");
 }
 if(on == false){
 Lock();
 }
 }
 
 function Reload () {
 yield WaitForSeconds (reloadTime);
 Fire();
 }
 
 function Lock () {
 if(on == true){
 Fire();
 }
 }
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
avatar image
1

Answer by bubzy · Jul 31, 2013 at 05:03 PM

looks like you made an infinite loop there buddy

 Fire()

calls

 Reload()

and then

 Reload()

calls

 Fire()

and then....... well, you get it.

also as a good practice tip (and a timesaver)

 if(canfire == true)

can be written as

 if (canfire)

and for a false value

 if (canfire == false)

can be written as

 if(!canfire)



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 Cbjfan1 · Jul 31, 2013 at 05:30 PM 0
Share

I'm trying to get fire to detect if on is false so it can send it back to Update, but I'm not sure if update is the right function I'm ai$$anonymous$$g for :/

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

17 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

Related Questions

Destroyed Object Error 2 Answers

CAN SOMEONE FINALLY HELP ME! 3 Answers

Argument cannot be Null??, 2 Answers

Null reference exception error in Unity, Please Help! 1 Answer

NullReferenceException Error 6 Answers


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