- Home /
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?
I would suggest:
Running the game
Instantiating a couple bullets
Pausing the Editor
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.
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.
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
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
![](https://koobas.hobune.stream/wayback/20220613141339im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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