Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 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 IceKapper85 · Apr 21, 2021 at 01:59 PM · instantiaterendererprefab-instanceinconsistent

Prefab set to destroy when outside of camera being destroyed on instantiation

Hi all! I've been having an issue with some code, where I'd like projectiles to vanish once they leave the screen. I've done this by checking if the sprite renderer is on screen each update and if not, destroy the object.

The problem with this is that it works when the player instantiates the projectile, it appears, travels and then vanishes when on screen. For some reason when I try to have one of the enemys instantiate the exact same projectile, the log for "is invisible" pops up immediatley and it's destroyed.

Any ideas or help are greatly appreciated, thank you!

     public new Renderer renderer;
 
     void Start()
     {
         renderer = GetComponentInChildren<Renderer>();
     }
 
     private void Update()
     {
         if (!renderer.isVisible)
         {
             Debug.Log("Object is not visible");
             Destroy(gameObject);
         }
     }

Like I mentioned above they should be instanting the same way, on the player with a click and the enemy on a timer like so

 //Player
     void Shoot1()
     {
         //The Projectile fired will change based on player input
         Instantiate(projectile1, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation);
     }
 
 //Enemys
            Count();
             if (currentTime >= spawnTime)
             {
                 Instantiate(projectile, new Vector3(transform.position.x, transform.position.y + spawnOffset,transform.position.z), transform.rotation);
                 currentTime = 0;
             }

Update:I have since removed and added my player character prefab to the scene without making any adjustments to the code and now the projectiles are being destroyed upon instantiation from the player character too.

I can only assume it's a technical issue above my understanding of Unity

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 IceKapper85 · Apr 21, 2021 at 03:04 PM

Solved! Found the answer to my issue here: https://answers.unity.com/questions/41962/renderisvisible-returning-false-for-one-frame.html

Turns out that the renderer is set to invisible for one frame when created, now working as intended :D

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
0

Answer by gigos22 · Apr 21, 2021 at 02:39 PM

I don't get 2 things here:

  1. Why aren't you just destroy the projectile once it reached it's target aka the enemy?

  2. Why are you instantiating the projectile twice..? It should be instantiated just once, and then travel to it's target, and as mention above, destroyed on impact.

Comment
Add comment · Show 3 · 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 IceKapper85 · Apr 21, 2021 at 02:42 PM 0
Share

Hi Gigo22! The projectile is destroyed once it hits the enemy, as well as if it hits a wall, etc, I'd like it so that the player can only damage enemy's when they are on screen and vice versa.

Also it's only instantiating once, the second bit of code just takes from both the player script and the enemy script just to show that there aren't any significant differences that I can see to how they're instantiated

avatar image gigos22 IceKapper85 · Apr 21, 2021 at 03:19 PM 1
Share

Well I'm glad you got it eventually.

avatar image IceKapper85 gigos22 · Apr 21, 2021 at 03:21 PM 0
Share

Thank you! I decided I prefer how it works without it, but it's all a fun learning expirience

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

140 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

Related Questions

How to instantiate many different prefabs from one prefab? 1 Answer

Changing two different objects renderer colour 1 Answer

instantiated GameObject Prefab jumps in air after instantiate 1 Answer

How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 1 Answer

Modifying/Calling Instantiated Objects 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