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 /
avatar image
0
Question by deancampagnolo · Jul 12, 2018 at 07:06 AM · respawnfixedupdatedestroy objectmissingreferenceexception

After destroying an object I respawn it and set a variable equal to it. However the next time Fixed Update loops it is equal to null, I have no idea why.

Basically after destroying my player my code re Instantiates it and resets my player variable to the new copy. However for the next Fixed Update player is set to null. After setting the player variable equal to the object in fixed update no problems arise afterwards.

As a side note I am a beginner when it comes to coding and unity so any suggestions would be great, thank you!

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class GameMaster : MonoBehaviour {

 public static GameMaster gm;// not entirely sure why this is here

 public static Transform playerPrefab;
 public static Transform beginningSpawnPoint;
 public Transform playerPrefabForInspector;
 public Transform beginningSpawnPointForInspector;
 public float fallOffMap;
 private static GameObject player;
 public static Vector2 rocketBlastOnPlayer;
 public static float minimumMomentumSpeed = 1f;//minimum speed needed to calculate momentum
 public static int momentumFactor = 20; // the speed divided by this is then subtracted to the speed (e.g. if speed is 30 with momentumFactor 4 then it would return 30 - (30/4);

 private void Awake() {
     forInspectorVars();
 }

 private void forInspectorVars() {//FIXME I am pretty sure that this isn't right
     playerPrefab = playerPrefabForInspector;
     beginningSpawnPoint = beginningSpawnPointForInspector;
 }

 private void Start() {
     player = GameObject.Find("Player");
     
 }

 public static void KillPlayer() {
     print(player.name);
     Destroy(player);
     RespawnPlayer(playerPrefab, beginningSpawnPoint);
     print(player.name);
 }

 public static void DamagePlayer(int amount) {
     if(player == null) {
         print("OHNO2");
     }
     player.GetComponent<MainC>().Damage(amount);
 }

 public static void DamageEnemy(GameObject NPCObject, int amount) {
     NPCObject.GetComponent<NPCStats>().Damage(amount);
 }

 public static void HealEnemy(GameObject NPCObject, int amount) {
     NPCObject.GetComponent<NPCStats>().Heal(amount);
 }

 public static void RespawnPlayer(Transform playerPrefab, Transform beginningSpawnPoint) {
     Instantiate(playerPrefab, beginningSpawnPoint.position, beginningSpawnPoint.rotation);
     player = GameObject.FindGameObjectWithTag("Player");
     if (player == null) {
         print("OHNO");
     }
 }

 public static void AddForceToNPC(GameObject theObject, Vector2 force) {
     theObject.GetComponent<Rigidbody2D>().AddForce(force, ForceMode2D.Impulse);
 }

 public static void AddRocketBlastOnPlayer(Vector2 newVelocity) {
     rocketBlastOnPlayer += newVelocity;
 }

 public static Vector2 GetRocketBlastOnPlayer() {
     return rocketBlastOnPlayer;
 }

 public static Vector2 CalculateXMomentum(Vector2 Momentum) {
     if (Momentum.x > minimumMomentumSpeed) {
         return new Vector2(Momentum.x - (Momentum.x / momentumFactor), Momentum.y);
     }
     return new Vector2(0, Momentum.y);
 }

 public IEnumerator IsFallOff() {
     while (true) {
    
     }
 }
 


 private void FixedUpdate() {
     //print(player.name);
     rocketBlastOnPlayer = CalculateXMomentum(rocketBlastOnPlayer);
     if(player == null) {//FIXME I DONT KNOW WHY THIS BELONGS HERE
         player = GameObject.FindGameObjectWithTag("Player");
     }
     if(player == null) {
         print("player is null");
     }
     /*if(player != null) {
         print("yay");
     }*/
 }

}

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 DawidNorasDev · Jul 12, 2018 at 08:56 AM 0
Share
 public IEnumerator IsFallOff() {
      while (true) {
     
      }
  }
 

This will hang your application. @$$anonymous$$ posted proper solution

1 Reply

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

Answer by $$anonymous$$ · Jul 12, 2018 at 08:14 AM

Try

 player = Instantiate(playerPrefab, beginningSpawnPoint.position, beginningSpawnPoint.rotation);




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

86 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

Related Questions

How do you destroy a prefab, but not the original object? 1 Answer

Respawn item if player dies 0 Answers

Javascript. Respawning a character after destroyobject(). 0 Answers

After destroying an object, I need it to respawn in a random area a couple seconds after the target is destroyed. 1 Answer

How to stop Unity from destroying prefab original when destroying copied instances? 2 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