• 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 ArkryxX · Mar 22, 2020 at 06:28 PM · errordestroygameobject

How do i fix this error?,I Have A Problem With The Destroy Command

when i have my game started and i got hit, there is an error that says:"The object of Type 'GameObject' has been destroyed but you are still trying to acces it. Your script should either check if it is null or you shold not destroy the Object"

here is my script that should destroy the Player, :

using System.Collections; using UnityEngine.SceneManagement; using System.Collections.Generic; using UnityEngine; public class Move2d : MonoBehaviour {

 public GameObject deadexplosion;
 public float speed;
 public float heavy;

 private Rigidbody2D rb2d;

 void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
 }
 void FixedUpdate()
 {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
     Vector2 movement = new Vector2(moveHorizontal, moveVertical);
     rb2d.AddForce(movement * speed / heavy);
 }

 void dead()
 {
     Destroy(GameObject.FindGameObjectWithTag("Player"));
 }

 void BackToMainMenu()
 {
     SceneManager.LoadScene("Mainmenu");
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.tag == "Enemy")
         dead();
     BackToMainMenu();
 }

};

And here is the script that has the error, the eroors is "Vector3 difference = target - Player.transform.position :

using UnityEngine;

public class PointAndShoot : MonoBehaviour { private Rigidbody2D rb2d;

 public GameObject crosshairs;

 public GameObject player;

 public GameObject bulletPrefab;

 public GameObject bulletStart;

 public float bulletprefab;

 public float bulletSpeed = 60.0f;

 private Vector3 target;
 void Start()
 {
     Cursor.visible = false;
 }
 private void Update()
 { 

     target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
     crosshairs.transform.position = new Vector2(target.x, target.y);

     Vector3 difference = target - player.transform.position;
     float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
     player.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ - 90.0f);

     if (Input.GetMouseButtonDown(0))
     {
         float distance = difference.magnitude;
         Vector2 direction = difference / distance;
         direction.Normalize();
         fireBullet(direction, rotationZ);
     }
 }
 void fireBullet(Vector2 direction, float rotationZ)
 {
     GameObject b = Instantiate(bulletPrefab) as GameObject;
     b.transform.position = bulletStart.transform.position;
     b.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
     b.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
 }

} I am realy noob and i am happy for every Response :) ,when i have my game started and i got hit, there is an error that says:"The object of Type 'GameObject' has been destroyed but you are still trying to acces it. Your script should either check if it is null or you shold not destroy the Object"

here is my script that should destroy the Player, :

using System.Collections; using UnityEngine.SceneManagement; using System.Collections.Generic; using UnityEngine; public class Move2d : MonoBehaviour {

 public GameObject deadexplosion;
 public float speed;
 public float heavy;

 private Rigidbody2D rb2d;

 void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
 }
 void FixedUpdate()
 {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
     Vector2 movement = new Vector2(moveHorizontal, moveVertical);
     rb2d.AddForce(movement * speed / heavy);
 }

 void dead()
 {
     Destroy(GameObject.FindGameObjectWithTag("Player"));
 }

 void BackToMainMenu()
 {
     SceneManager.LoadScene("Mainmenu");
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.tag == "Enemy")
         dead();
     BackToMainMenu();
 }

};

And here is the script that has the error, the eroors is "Vector3 difference = target - Player.transform.position :

using UnityEngine;

public class PointAndShoot : MonoBehaviour { private Rigidbody2D rb2d;

 public GameObject crosshairs;

 public GameObject player;

 public GameObject bulletPrefab;

 public GameObject bulletStart;

 public float bulletprefab;

 public float bulletSpeed = 60.0f;

 private Vector3 target;
 void Start()
 {
     Cursor.visible = false;
 }
 private void Update()
 { 

     target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
     crosshairs.transform.position = new Vector2(target.x, target.y);

     Vector3 difference = target - player.transform.position;
     float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
     player.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ - 90.0f);

     if (Input.GetMouseButtonDown(0))
     {
         float distance = difference.magnitude;
         Vector2 direction = difference / distance;
         direction.Normalize();
         fireBullet(direction, rotationZ);
     }
 }
 void fireBullet(Vector2 direction, float rotationZ)
 {
     GameObject b = Instantiate(bulletPrefab) as GameObject;
     b.transform.position = bulletStart.transform.position;
     b.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
     b.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
 }

} I am realy noob and i am happy for every Response :)

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 ArkryxX · Mar 22, 2020 at 10:28 AM 0
Share

i saw that the } and the using Things are glitched out in the Question, sorry for that:D

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.

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

236 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The object of type 'GameObject' has been destroyed but you are still trying to access it. 0 Answers

Unity won't load. Stops at starting server on unity package manager. 3 Answers

2018.2 text mesh pro. plugin Errors. 7 Answers

Unity error -> crashing while build & run 0 Answers

Unity won't create or open any projects 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