• 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 coldmetalhands · Mar 06 at 10:02 PM · instantiateprefabs

No code running after Instantiate is called

I have created a cannon that fires missiles by spawning prefab missiles. when I put the Rigidbody2D.AddForce() function in the Start function of the missile, the missile will move. However, when I place the code in the Cannon script that instantiates the prefabs, it will not run.

I've been able to deduce that all code after I call Instantiate() will not run (I put some Debug.Log before and after and the ones after never get called).

Does anyone know why this is happening?

Cannon Code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Cannon : MonoBehaviour
 {
     private bool active=false;
     public Rigidbody2D missile;
     public GameObject cannonOrigin;
     private Animator anim;
     // Start is called before the first frame update
     void Start()
     {
         anim=gameObject.GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space)){
             active=!active;
             
         }
         if(active){
             anim.Play("Cannon-Active");
         }else{
             anim.Play("Cannon-Deactivate");
         }
         if((Input.GetKeyDown(KeyCode.LeftControl)||Input.GetKeyDown(KeyCode.RightControl))&&active){
            Rigidbody2D m = Instantiate(missile, cannonOrigin.transform.position, cannonOrigin.transform.rotation);
             Debug.Log("Fire!");
             m.AddForce(transform.forward*20);
             
         }
         
     }
 }
 


Missile Prefab Code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Missile : MonoBehaviour
 {
     // Start is called before the first frame update
     private Rigidbody2D rgbd;
     public GameObject explosion;
     void Start()
     {
         rgbd = gameObject.GetComponent<Rigidbody2D>();
         //rgbd.AddForce(new Vector3(1000,0,0));
         //rgbd.velocity = transform.forward*100;
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
     private void OnCollisionEnter2D(Collision2D other) {
         Destroy(gameObject);
         Instantiate(explosion, transform.position, Quaternion.identity);
     }
 }
 <br><br>

m.AddForce() never gets called, nor does the Debug.Log() preceding it. It is as if the code block just ends after the object is instantiated. Does anyone know what is going on here?

Comment
Add comment · Show 4
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 sacredgeometry · Mar 07 at 08:14 AM 0
Share

Does implicitly casting the Object to a RidgedBody2D select the RidgedBody component on a Gameobject? Line 30 of the Canon.

If so you learn something new every day. Its pretty gross if it does though.

avatar image coldmetalhands · Mar 07 at 04:11 PM 0
Share

I would have thought you cannot, but according to this (Instantiating projectiles & explosions section) you can and should. The reasoning being that this ensures whatever object is passed does indeed have the required component (Rigidbody in this case). I originally set it up using gameObject.GetComponent to assign the rigidbody2D to m, but after seeing you can implicitly cast to just grab the Rigibody2D right off the bat, I changed to that (all the cool kids are doing it).

Regardless, both gameObject.GetComponent() and straight up casting Rigidbody2D had the same result (not working). No errors, just the code exiting immediately after calling instantiate. The prefab does populate, but then sadly falls to the floor instead of blasting off as it should.

avatar image xxmariofer coldmetalhands · Mar 07 at 07:07 PM 0
Share

totally my bad i thougth you were instantiating gameobjects, ill remove my coment

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by coldmetalhands · Mar 07 at 05:16 PM

Oddly enough, I got this working again after switching back to GameObject as the prefab type and getting the Rigidbody with GetComponent in the Update method after calling Instantiate. I noticed I was getting a "Type mismatch" in the inspector. I guess Unity doesn't like that, but not enough to throw any errors over it. (No idea why this did not work originally).

Not sure why the manual for prefabs says you can cast them that way though.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Cannon : MonoBehaviour
 {
     private bool active=false;
     public GameObject missile;
     public GameObject cannonOrigin;
     private Animator anim;
     // Start is called before the first frame update
     void Start()
     {
         anim=gameObject.GetComponent<Animator>();
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space)){
             active=!active;
             
         }
         if(active){
             anim.Play("Cannon-Active");
         }else{
             anim.Play("Cannon-Deactivate");
         }
         
         if((Input.GetKeyDown(KeyCode.LeftControl)||Input.GetKeyDown(KeyCode.RightControl))&&active){
             Debug.Log("Fire1!");
             Rigidbody2D m = Instantiate(missile, cannonOrigin.transform.position, cannonOrigin.transform.rotation).GetComponent<Rigidbody2D>();
             Debug.Log("FIRED!");
             m.AddForce(new Vector3(1000,0,0));
             
         }
     }
 }

Comment
Add comment · Show 2 · 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 Eno-Khaon · Mar 07 at 07:46 PM 0
Share

As a point of reference, how does it compare if you use the Generic variant of Instantiate() instead?

 Rigidbody2D missile;
 // ...
 Rigidbody2D m = Instantiate<Rigidbody2D>(missile, cannonOrigin.transform.position, cannonOrigin.transform.rotation);
avatar image coldmetalhands Eno-Khaon · Mar 07 at 08:19 PM 0
Share

It doesn't seem to like that either. It looks like trying to take a shortcut results in a type mismatch occurring on the Cannon game object in the inspector. I guess I'll just keep it as type GameObject for now, since it is working an just adds on small step of getting the component.

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

164 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

Related Questions

How can you make a Instanciate prefab follow a scene object? 1 Answer

Instantiating Prefabs from a MonoBehaviour-derived class using an array -> compiling but not rendering [SOLVED] 0 Answers

How to instantiate and destroy objects with 2d trigger 1 Answer

Question about instatiating prefab/clone character and how they work/interact with non prefeb buttons 0 Answers

how to call the animation of prefab 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