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 /
  • Help Room /
avatar image
0
Question by Xilver266 · May 17, 2016 at 11:16 AM · c#space shootershield

Cant take another shield after took one

Hi, im scripting a shield system for space shooter and when i take a shield the first time the script works correctly but when take another shield, i get some errors.

 void DestroyShields()
 {
     GameObject[] shields = GameObject.FindGameObjectsWithTag("Shield");
     for (var i = 0; i < shields.Length; i++)
     {
         Destroy(shields[i]);
     }
 }
 
 void OnTriggerEnter(Collider other)
 {
 
     if (other.CompareTag("ItemShield"))
     {
 
         Destroy(other.gameObject);
         DestroyShields();
 
         Instantiate(shieldController, transform.position, shieldController.transform.rotation);
         GameObject shieldControllerObject = GameObject.FindWithTag("Shield");
         shieldObject = shieldControllerObject.GetComponent<ShieldController>(); // I think this is not reassigning the new shield
 
         shieldObject.Blink(); //<- Here the code dont execute at second shield
         shield = true;
         shieldHealth2 = shieldHealth;
 
     }
     [...]
     if (shield && shieldHealth2 > 0) {
         Destroy(other.gameObject); // Destroys the enemy
         shieldHealth2--;
         shieldObject.Blink(); // <- Here the code fails
         if (shieldHealth2 == 0) {
             DestroyShields();
             shield = false;
         } else {
                     /* Game Over */
         }
     }
 }



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

2 Replies

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

Answer by Xilver266 · May 17, 2016 at 05:22 PM

Hi

I solved changing this:

 GameObject shieldControllerObject = GameObject.FindWithTag("Shield");
  shieldObject = shieldControllerObject.GetComponent<ShieldController>();

for this:

 shieldObject = Instantiate(shieldController, transform.position, shieldController.transform.rotation) as ShieldController;

thanks for the clues, i was blocked :)

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
avatar image
1

Answer by NoseKills · May 17, 2016 at 03:20 PM

It's a bit difficult to understand exactly what's going on without knowing for example what is the name of the attached class. Is this ShieldController? What is the type of the variable 'shieldController' and is it a prefab assigned in inspector?

Anyways i think most of the problems are caused here.

 DestroyShields();
 Instantiate(shieldController, xxx, zzz);

 GameObject shieldControllerObject = GameObject.FindWithTag("Shield");
 shieldObject = shieldControllerObject.GetComponent<ShieldController>();


Destroy() doesn't happen instantly. So if you are trying to first destroy all 'shields' then create a new one and find it with 'FindWithTag("Shield");', you might actually find one of the old shields marked for destruction.

If I understand right and you indeed try to FindWithTag() the thing you just instantiated, that's unnecessary. Instantiate returns the new object you Instantiated so just take a reference to it

 GameObject shieldControllerObject = (GameObject)Instantiate(shieldController, xxx, zzz);
 shieldObject = shieldControllerObject.GetComponent<ShieldController>();

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Attach a shield to player (C#) 2 Answers

Trail effect for non-moving object 2 Answers

The bolt doesn't move 0 Answers

[Solved] I am trying to build a space sim/ shooter type game. 1 Answer

space shooter tutorial help 0 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