• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

how do i fix all compiler errors so i can enter playmode? 4 Answers

Array index is out of range. 1 Answer

not all code paths return a value? 1 Answer

Errors in code 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges