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 InfixionGames · Oct 28, 2018 at 06:07 PM · scripting problemprefabreference

How to access a script on a instantiated prefab

So I have a SpawnManager that is already in the scene view. And after 50 kills it spawns a boss prefab. I want to be able to change the boss prefab script variables with my SpawnManager script.

For example. In my SpawnManager script I say that if the boss has less than 50 health, make text appear.

I already tried GameObject.Find and GetComponent. But I always get the Object reference not set to an instance of an object error.

Thanks in advance.

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
2

Answer by Collect · Oct 28, 2018 at 06:24 PM

  • I highly suggest avoiding this completely by attaching the health, and the check for the bosses health to the boss itself, rather than being managed by an outside source.

  • That being said, your issue is a typical problem of not setting a reference to your instantiation, because instead of trying to find the created boss, it's trying to find the prefab.

  • To fix this make sure that you're setting the reference for the created boss upon instantiation. Now, you can access the GetComponents, like your boss script, from the reference. alt text


bossspawn.png (20.7 kB)
Comment
Add comment · Show 5 · 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 InfixionGames · Oct 28, 2018 at 07:13 PM 0
Share

The reason I can't add it to the boss is because when the boss dies a coroutine has to start. And when the boss dies the GameObject is destroyed. I don't get an error anymore but the coroutine doesn't start

(I know that the code is messy)

$$anonymous$$y UImanager code:

  public void Update()
     {
         
         GameObject spawn_$$anonymous$$anager = GameObject.Find("Spawn_$$anonymous$$anager");
         Spawn$$anonymous$$anager spawnmanager = spawn_$$anonymous$$anager.GetComponent<Spawn$$anonymous$$anager>();
         if (livesImageDisplay.sprite == lives[0])
         {
             score = 0;
 
         }
         if (score >= 5)
         {
             if (spawned == false)
             {
                 GameObject spawnedBoss = Instantiate(bossBakx, new Vector3(7.7f, transform.position.y), Quaternion.identity);
                 bossBakxScript = bossBakx.GetComponent<Boss>();
                 spawned = true;
                 spawnmanager.bossSpawned = true;
                 
             }
         }
  
 
         if (bossBakxScript.bossHealth <= 0)
         {
             StartCoroutine(illBeBakx());
         }

$$anonymous$$y boss Code:

     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Laser")
         {
             bossHealth = bossHealth - 5;
             Debug.Log(bossHealth);
             Destroy(other.gameObject);
             if (bossHealth <= 0)
             {
                 GameObject[] book;
 
                 book = GameObject.FindGameObjectsWithTag("BakxBook");
                 foreach (GameObject Book in book)
                 {
                     Destroy(Book);
                 }
 
                 GameObject spawn_$$anonymous$$anager = GameObject.Find("Spawn_$$anonymous$$anager");
                 Spawn$$anonymous$$anager spawnmanager = spawn_$$anonymous$$anager.GetComponent<Spawn$$anonymous$$anager>();
                 spawnmanager.bossSpawned = false;
                 Destroy(other.gameObject);
                 Destroy(this.gameObject);
             }
         }

Thanks again

avatar image Collect InfixionGames · Oct 28, 2018 at 07:27 PM 0
Share

I'm not sure what the coroutine llBeBakx(); does, but it could possibly be placed in your Spawn$$anonymous$$anager, and run there. That way even after the boss dies your coroutine continues.

avatar image InfixionGames Collect · Oct 28, 2018 at 07:43 PM 0
Share

So I did what you said and placed the coroutine in the spawnmanager and the text appears but it doesn't disappear.

Here is my coroutine:

     public IEnumerator illBeBakx()
     {
         Debug.Log("Point A");
         GameObject.Find("I'll be bakx.").transform.localScale = new Vector3(1, 1, 1);
         yield return new WaitForSeconds(2);
         Debug.Log("Point B");
         GameObject.Find("I'll be bakx.").transform.localScale = new Vector3(0, 0, 0);
     }

(The Debug.Log is only to see if it works. And only point A is printed) The I'll be bakx. is just a textobject in the Canvas

Thanks for the quick responses.

Show more comments

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

187 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

Related Questions

Object reference not set to an instance of an object (Scripts calling objects which only exist in certain scenes) 1 Answer

How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer

Creating a Prefab and Keeping Original Objects Script Information 0 Answers

One Script Referred by Another Script used by Multiple Objects 0 Answers

How do I get a prefab to know who made it? 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