Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 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 Hadi563 · 4 days ago · prefab2d gamegameobject.find

Why can't GameObject.Find find more than one object at the start function?

Okay so i'm setting up this real quick 2d game where you can only move left and right and catch objects falling from the top of the screen. i decided to add 3 hearts at the top right side of the screen and everytime you miss an object one heart dissapears. In the "Spawnables" Script applied to all the different prefabs that fall from the sky, i declared a float variable called objectsMissed, now everytime an object reaches -6 on the y axis it will be destroyed since it's not on the screen anymore and we add 1 to the objectsMissed variable. The problem is these objects are prefabs and you cannot assign gameObjects to prefabs so i have to find them through script at the start function, the first heat(furthest right) dissapears when i first miss an object, but when i miss a second it gives me this error(NullReferenceException: Object reference not set to an instance of an object). Here's my code.

 public float objectsMissed;
 
     private GameObject heart1;
     private GameObject heart2;
     private GameObject heart3;
 
     void Start()
     {
         objectsMissed = 0;
         
         heart1 = GameObject.Find("HeartR");
         heart2 = GameObject.Find("HeartM");
         heart3 = GameObject.Find("HeartL");


      void Update()
     {
         if (transform.position.y <= yBounds)
         {
             Destroy(gameObject);
             objectsMissed++;
         }
 
         if (objectsMissed == 1)
         {
             heart1.SetActive(false);
         }
         else if(objectsMissed == 2)
         {
             heart2.SetActive(false);
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hellium · 4 days ago

Having the falling objects handle the hearts is not the way to go.

You need to rework the architecture of your game. Suggested architecture:

CODE NOT TESTED


   1. A Life script, placed in your scene, handling the hearts

 public class Life : MonoBehaviour
 {
      // Drag & drop the hearts in the inspector
      [SerializeField] private GameObject[] hearts;
 
      private int amount;
 
      public int Amount => amount;
 
      private void Awake()
      {
          amount = hearts.Length;
      }
 
      public void Decrease()
      {
           if(amount == 0) return;
 
           hearts[amount - 1].SetActive(false);
           amount--;
      }
 }


   2. A Spawner script, placed in your scene, handling the spawn of your objects. You should already have something similar

 public class Spawner : MonoBehaviour
 {
      // Drag & drop in the inspector the prefab to spawn, replace `Spawnee` by the name of your script
      [SerializeField] private Spawnee prefab;
 
      // Drag & drop the object with the Life component in the inspector
      [SerializeField] private Life life;
 
      private IEnumerator spawnCoroutine;
 
      private void Start()
      {
          spawnCoroutine = Spawn();
          StartCoroutine(spawnCoroutine);
      }
 
      private IEnumerator Spawn()
      {
          WaitForSeconds wait = new WaitForSeconds(5);
          while(true)
          {
               yield return wait;

               // Consider using a pool instead of destroying and instantiating
               Spawnee spawnee = Instantiate(prefab);
               spawnee.Despawned += OnSpawneeDespawned;
          }
      }
 
      private void OnSpawneeDespawned(Spawnee spawnee)
      {
          spawnee.Despawned -= OnSpawneeDespawned;
          life.Decrease();
          if(life.Amount == 0)
              StopCoroutine(spawnCoroutine);
      }
 }


   3. A Spawnee script, placed on your prefab you instantiate. Basically the script you have in your question

 public class Spawnee : MonoBehaviour
 {
      [SerializeField] private float yBounds;
 
      public event System.Action<Spawnee> Despawned;
 
      void Update()
      {
          if (transform.position.y <= yBounds)
          {
              Despawned?.Invoke(this);
              Destroy(gameObject);
          }
      }
 }
Comment
Add comment · Show 2 · 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 Hadi563 · 3 days ago 0
Share

@Hellium i can't reference the spawnee script cz there are 4 spawnable items, in my game manager script(the spawner) i created a list and placed all the 4 items in it so they spawn randomly, is there a way to call the spawnee scripts even if it is attached on 4 different prefabs?

avatar image Hellium Hadi563 · 3 days ago 0
Share

You need to tweak the Spawner according to your needs. Since you haven't provided your spawner code in the first place, I had to go with a simple version of it, but I'm sure you'll figure how to merge my spawner script into yours ;)

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

228 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

Related Questions

How to move Instantiated 2D objects by 0.5 using arrows 1 Answer

Prefab C# : Level Restart Alert problem : Android 2 Answers

Controlling Animator through Script. I'm stuck please help! 0 Answers

Instantiated Prefab not moving along transform points 1 Answer

Instantiate prefab with different values each time 4 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