Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Shire_ · May 11, 2016 at 02:54 PM · noobbulletsmissingreferenceexceptionclonesturrets

Each bullet forms clone of the last

Hi, firstly I am relatively new to unity and C# in general but I am trying to create a simple 3D ball rolling platform game with a turret that shoots the player.

Currently the turret will wake up once the player is within shooting range and continue to shoot cannonballs are regular intervals. However, each cannonball forms a clone of the last one fired so it shows this in the hierarchy;

cannonballs(clone)
cannonballs(clone)(clone)
cannonballs(clone)(clone)(clone)
cannonballs(clone)(clone)(clone)(clone)
etc.

http://imgur.com/OSFP8BE

I created a cannonball destroy script to remove the cannonball clones from the game after a set amount of time and attached this script to the cannonballs prefab as seen here;

public class CannonBallDestroy : MonoBehaviour {

 public float DeathTime;

 void Update()
 {
         Destroy(gameObject, DeathTime);
     }

 }

However, with this script in place a new error appears in the console each time the turret tries to shoot a cannonball;

MissingReferenceException: The object of type 'GameObject' 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.

http://imgur.com/hhUjS5k

As far as I understand, it appears to be destroying the prefab instead of the cannonball clones?? I am not sure what I am doing wrong here and any help would be greatly appreciated. I have tried incorporating the destroy game object into the main turretAI script and I have tried destroying the gameobject by name instead but I have had no luck and I am very confused.

Here is the main turretAI script;

public class TurretAI : MonoBehaviour {

 //floats
 public float distance;
 public float wakeRange;
 public float LastShotTime = float.MinValue;
 public float fireRate;

 //turretspin
 public float turretSpeed;

 //booleans
 public bool awake = false;

 //references
 public GameObject Cannonballs;
 public Transform target;
 public Animator anim;
 public GameObject BulletSpawn;
   

 void Awake()
 {
     anim = gameObject.GetComponent<Animator>();
 }


 void Update()
 {
   
 anim.SetBool("Awake", awake);
     RangeCheck();

     if (awake)
     {
         {
             //Rotate turret to look at player.
             Vector3 relativePos = target.position - (transform.position);
             Quaternion rotation = Quaternion.LookRotation(relativePos);
             rotation.x = 0;
             rotation.z = 0;
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turretSpeed);
         }
     }

     //fire rate
     if (distance < wakeRange && Time.time > LastShotTime + (3.0f / fireRate))
     {
         LastShotTime = Time.time;
         launchBullet();
     }
 } 

     void launchBullet()
  {

     Cannonballs = Instantiate(Cannonballs, BulletSpawn.transform.position, BulletSpawn.transform.rotation) as GameObject;
     
     Cannonballs.GetComponent<Rigidbody>().AddForce(transform.forward * 500);
  
 }

 void RangeCheck()
 {
     distance = Vector3.Distance(transform.position, target.transform.position);

     if (distance < wakeRange)
     {
         awake = true;
     }
     if (distance > wakeRange)
     {
         awake = false;
     }
 } 


Thanks for reading

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

0 Replies

· Add your reply
  • Sort: 

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

57 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 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 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

Change bullet trajectory slightly towards new player position 0 Answers

Creating a preview block for a building game[help] 0 Answers

Scripting Errors 1 Answer

Solid Object in 2D 1 Answer

no error message but script wont work 0 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