Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ipk4fun_ · Nov 11, 2019 at 02:36 AM · 2d gamearraysarraylist

My arrays automatically empty out there objects.

this is a 2d game, with a health system. the code is used for the the health system is this;

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

public class HealthSystem : MonoBehaviour {

 public int health;
 public int numofhearts;

 public Image[] hearts;
 public Sprite fullHeart;
 public Sprite emptyHeart;

 private void Update()
 {
     if (health > numofhearts)
     {
         health = numofhearts;
     }
     for (int i = 0; i < hearts.Length; i++)
     {
         if (i < health)
         {
             hearts[i].sprite = fullHeart;

         }
         else
         {
             hearts[i].sprite = emptyHeart;
         }


         if(i < numofhearts)
         {
             hearts[i].enabled = true;
         }
         else
         {
             hearts[i].enabled = false;
         }
     }
     
 }

}

But when i fill up the array with the images its works until i respawn using these 2 codes;

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

public class Player : MonoBehaviour {

 public HealthSystem healthSystem;
 private HealthSystem health;

   
 
 void Update()
 {
     
     if(transform.position.y<= -20)
     {
         DamagePlayer(20);
     }
     
     
 }

 

 public void DamagePlayer(int damage)
 {
     healthSystem.health -= damage;
     if (healthSystem.health <= 0)
     {
         GameMan.killPlayer(this);
     }
 }

}

And;

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

public class GameMan : MonoBehaviour { private HealthSystem health; public HealthSystem healthSystem;

 void Update()
 {
   
 }
 public GameObject Enter;


 public void EnterIn()
 {
     Enter.SetActive(true);
 }





 public static GameMan gm;

 void Start()
 {
     if (gm == null)
     {
         gm = GameObject.FindGameObjectWithTag("GameMan").GetComponent<GameMan>();
     }
 }

 public Transform playerPrefab;
 public Transform spawnPoint;
 public int spawnDelay = 2;


 public IEnumerator RespawnPlayer()
 {
     yield return new WaitForSeconds(spawnDelay);
     Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
     

 }

 

 public static void killPlayer(Player player)
 {
     Destroy(player.gameObject);
     gm.StartCoroutine(gm.RespawnPlayer());

 }


}

Please help me fix these problem, thank you

Before i die

alt text

After i die and respawn

alt text

screen-shot-2019-11-10-at-93252-pm.jpg (233.8 kB)
screen-shot-2019-11-10-at-93425-pm.jpg (185.2 kB)
Comment
Add comment · Show 3
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 dacarrera · Nov 11, 2019 at 02:47 AM 1
Share

Is the playerPrefab that you're Instantiating in RespawnPlayer() what has the HealthSystem component attached? If so, perhaps double check the actual prefab (and not what you're editing in the hierarchy) and make sure it's setup correctly. There's always a chance you edited the reference in the hierarchy and it isn't saved to the prefab.

avatar image ipk4fun_ dacarrera · Nov 11, 2019 at 03:18 AM 0
Share

Yeah it is but it is still not working

avatar image ipk4fun_ · Nov 11, 2019 at 03:29 AM 0
Share

Also i cant add the images to the prefab

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by GrayLightGames · Nov 11, 2019 at 05:09 AM

Hi @ipk4fun_, I noticed in your screenshot that the images you are linking to are bold in the inspector link box... this indicates a difference between the object and the prefab. Select the prefab itself and look what is there in the list. I am guessing that they are all None there because you can't link prefab variables to objects outside the prefab. So because the prefab has an empty list, so will the player when you instantiate!

If I'm right, here's an option for you: Store and manage the hearts list in a different object that is never destroyed. There's no reason to store the health info on the player... and if you do for some reason you don't need to store the list of images. When the player respawns or is damaged, you can call a function on the Healthsystem object to update the heart container images based on the health and the number of hearts. If you store a reference to the Healthsystem on GameMan, you should be able to access it from either GameMan or the Player. And you can reset health or num of hearts in the respawn function. Hope that helps! I can elaborate if you need, but I'm pretty sure that is the source of the issue.

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 GrayLightGames · Nov 11, 2019 at 05:09 AM 0
Share

Also, don't die! :) Hang in there, there's always a solution!

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

147 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

Related Questions

Type function(): Object[] does not support Slicing 1 Answer

Material Array list and its .length not working/showing in dropdown menu 1 Answer

Client Getting Tile Info on Top Down Game 0 Answers

Help with for loop and arrays 3 Answers

Linear Interpolation of Array Values 1 Answer


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