• 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
1
Question by joeldamole0 · Jul 30, 2019 at 06:07 AM · gameobjectinstantiate prefab

Instantiate GameObject just doesn't appear

Hey, so I have a script on my player object which is supposed to instantiate a "bullet" prefab GameObject at the player in the direction of the targetPoint.

The issue I'm having is that there just is no GameObject instantiated that I can tell. Nothing appears in the Hierarchy or the scene. I am certain that the code is running as the debug statements are logged in the console.
The instantiation did work at one point before I started using physics/before the bullet had a Rigidbody2D component. (I was just setting positions and moving towards them, so the bullet didn't need a Rigidbody2D component).
I have linked the public GameObject "projectile" to the bullet prefab GameObject and the targetPoint the relevant transform.

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FireBullet : MonoBehaviour
 {
 
     public GameObject projectile;           // The GameObject to instantiate the bullet object
     private Transform targetPoint;          // The object to shoot at
     public float bulletSpeed = 100f;        // Public variable for the bullet speed
   
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButtonDown("Shoot"))        // When the space bar is pressed down
         {
             // Clone the Bullet @ the player
             GameObject clone = Instantiate(projectile) as GameObject;   // THIS DOESN'T SEEM TO WORK
             clone.transform.position = new Vector2(transform.position.x, transform.position.y);
             
             // Pointing bullet towards mouse
             clone.transform.LookAt(targetPoint, new Vector3(0, 0, -1));
 
             // Calculating the direction the bullet should travel, i.e. towards the mouse position
             Vector2 targetDirection;
             targetDirection.x = targetPoint.position.x - clone.transform.position.x;
             targetDirection.y = targetPoint.position.y - clone.transform.position.y;
               
             // Setting the bullet in motion in the calculated direction 
             // at the bulletSpeed as defined by the public variable "bulletSpeed"
             clone.GetComponent<Rigidbody2D>().velocity = targetDirection.normalized * bulletSpeed;
             Debug.Log("Fire Bullet! @ " + targetPoint.position);
         }
     }
 
 }


Any idea on how I can make the bullet just appear at the very least???


Edit: So I change the method to FixedUpdate(). On the bullet prefab I have a script which is just checking for collisions. Now when I shoot I'm getting a response that it has collided with my player (which is expected, as I'm spawning the bullet on the player). This obviously means that something is appearing and colliding but I still get nothing in the hierarchy or scene?

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
0

Answer by AlphaSilverback · Jun 21, 2020 at 10:36 PM

This may be a question just for a sanity check, but could it be, that you have already created a script on the projectile you instantiate, which destroys itself as soon as it hits something (it would make sense if you don't want it to ricochet around)?


This would explain why it reacts and compiles in code, and why you don't see it in the hierarchy. You can debug this by adding a Debug.Log() message to the function you assume destroys the bullet. It might help to write down a couple of other hypothesis to where your code is doing stuff like this, and following them sequentially.


You can also test if it is your bullet that does something weird by replacing the bullet with another/completely new prefab, that you know doesn't kill itself (process of elimination).


Debugging can be hard, but good debug messages, and good drawings of your design patterns and software architecture, can help you a long way.


If you are absolutely certain that none of your own scripts are killing the bullet GameObject, you can post this issue as a bug and upload your project to the Unity Devs. Please keep in mind, that you should only do this, if you can recreate the problem you are experiencing in a new fresh scene, with new and fresh scripts. If you are not able to isolate the bug and recreate it, it is very likely not a bug, but something you have coded.


I hope this helps.
Sincerely,
/Alpha

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Instantiated objects appear invisible. 0 Answers

Object reference not set to an instance of an object when trying to instantiate 0 Answers

How to I make a gameobject follow a list of nodes and to link an instantiated gameobject to a script. 0 Answers

All instantiated objects having same location, 0 Answers

Delete gameobject clone after a new one spawnes. 2 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