Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by sith4life · Mar 27, 2014 at 09:08 PM · c#instantiatesprite

Instantiated Sprite is not rendering.

I am having an issue with Instantiate. // ************************** Shoot Logic *********************************

     // make sure we have ammo and the shoot is not on cooldown
     if(Ammo > 0 && shootTimer <= 0)
     {
         // shoot timer is reset so it's ok to fire.
         if(Input.GetButtonUp("Fire1"))
         {
             Ammo = -1;
             shootTimer = 1.0f;    
             Vector2 direction; // direction the player is facing
             Vector2 gunPos; // position of the Gun on the player
             // which direction is the player travelling / facing?
             if(Input.GetAxis(axisName) < 0)
             {
                 // set the direction the bullet will travel to left
                 direction = -Vector2.right;
                 gunPos = new Vector2 (GameObject.FindGameObjectWithTag("Gun").transform.position.x - 1, // + 1 to stop the bullet from hitting the player
                                       GameObject.FindGameObjectWithTag("Gun").transform.position.y);
             }    
             else
             {    // set the direction the bullet will travel to right
                 direction = Vector2.right;
                 gunPos = new Vector2 (GameObject.FindGameObjectWithTag("Gun").transform.position.x + 1, // + 1 to stop the bullet from hitting the player
                                       GameObject.FindGameObjectWithTag("Gun").transform.position.y);
             }
             // set the bullet to "face" the same direction as the player
             Quaternion quaternionDirection = Quaternion.LookRotation(direction);
             // spawn the bullet
             Instantiate(bullets,gunPos, quaternionDirection);
         }
     }
     else
     {
         // bullet is on Cooldown so let's reflect the passage of time in the timer
         shootTimer -= Time.deltaTime;
     }

The issue is that bullets are not rendering in the Scene or Game views. When I drag the prefab into the scene it renders fine. The "bullet" is instantiating because "Bullet(Clone)" shows up in the Hierarchy. I also tried it with a standard cube mesh and that shows up just fine.

Script attached to bullet: float speed = 10.0f;

 void Update()
 {
     transform.position += transform.forward * speed * Time.deltaTime;
     Destroy (this.gameObject, 20.0f); // destroy after 20 seconds just in case
 }

Sprite Components:

  • 2D BoxCollider

  • Sprite Renderer (order in layer: 10)

  • Script

In short, the sprite is not rendering when instantiated.

Any assistance would be greatly appreciated! Thanks

EDIT 3/29/2014: Upon further examination of this issue, Thanks again Key_Less, I noticed that the projectile is "flying off" at some weird angle when I paused the game the Bullet's position was X: -6000 Y: -1100 Z: 0 and rotatation x: 0 y: 270 Z: 0. I'm certain the Quaternion is not "working correctly", can anyone provide any insight into why this is?

Comment
Add comment · Show 2
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 Key_Less · Mar 28, 2014 at 12:19 AM 1
Share

I would suggest:

  1. Running the game

  2. Instantiating a couple bullets

  3. Pausing the Editor

  4. Checking the instantiated bullet's properties

Then, Look at the bullet in the scene view and make sure it has the expected rotation/position values, and is indeed within the camera's view frustum. Check your Camera's culling mask too, just in case. Often times it's the little things that are overlooked first, hopefully it is something as simple as this.

I hope this can help you make some progress.

avatar image sith4life · Mar 28, 2014 at 03:09 AM 1
Share

thanks $$anonymous$$ey_Less! I paused the game and noticed the sprite was rotated 270* on the Y-Axis. I changed it to zero and it showed up properly. I think I'm doing the Quaternion improperly.

1 Reply

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

Answer by sith4life · Apr 06, 2014 at 06:45 PM

The issue was that the sprite was being rotated 270 degree on the Y axis. I am posting this answer so that if anyone comes across a similar problem they can try ad troubleshoot the problem the same way

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 Lao2211 · Sep 25, 2016 at 10:31 PM 0
Share

Confir$$anonymous$$g it works. But rather than rotate the instantiated GameObject by 270 just set the second parameter of SetParent to true:

 instance.transform.SetParent(Canvas_or_whatever.transform, true);

Example from my own project:

 GameObject animated_ui_element = Instantiate(Resources.Load<GameObject>("anims/button_rays_prefab"));
 animated_ui_element.transform.SetParent(GameObject.Find("Canvas").transform, true);

$$anonymous$$y animated_ui_element (a sprite with some frames) didn't appeared on the Canvas until I parented it that way.

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

22 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

Related Questions

Problem with Line Renderer 1 Answer

Distribute terrain in zones 3 Answers

How can I set a sprite to a bunch of clones? 1 Answer

Multiple Cars not working 1 Answer

Instantiating multiple sprites and assigning different colors for each 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