Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 question was closed Dec 14, 2016 at 03:26 AM by mmorro1222 for the following reason:

Problem Solved

avatar image
0
Question by mmorro1222 · Dec 14, 2016 at 03:16 PM · instantiatetriggeraiparentchild

Instantiate with parent not working?

I'm not sure what happened to cause this, but for some reason the instantiated object I'm creating is not being instantiated with a parent. Basically, I'm making a 3d game somewhat along the lines of the classic game Asteroids. In the game there is a player ship as well as enemy ships which fire at the player. When the player clicks it instantiates a torpedo with the following code:

 if (Input.GetButtonUp("Fire1"))
             {
                 Instantiate(torpedo, transform.position, transform.rotation, transform);
             }

In order to prevent the torpedo from just exploding on the player's ship I had to make sure it was instantiated as a child of the player and then in it's onTriggerEnter check if the collider is that of the parent(playership). This works fine for the player, however, when the enemy ships instantiate torpedos of their own with the same call to instantiate, the instantiated torpedos are not instantiated with the enemy ship as their parent, causing the torpedo to explode on the enemy ship. I have checked this with a debug log. Oddly enough, it seems that the torpedo is being set as a child of the enemy ship in the hierarchy, however the debug check(along with the if statement) I've set up refuses to acknowledge that.

Enemy ship instantiate call(checks if raycast hits player before firing):

 void attack()
     {
         RaycastHit hit;
         if (Physics.Raycast (transform.position, transform.forward, out hit))
         {
             if(hit.collider.gameObject.tag == "Player")
             {
                 Instantiate(torpedo, transform.position, transform.rotation, transform);
                 StartCoroutine(ChargeTorpedo());
             }
         }
     }

and the torpedo's script:

 using UnityEngine;
 using System.Collections;
 
 public class TorpedoManager : MonoBehaviour {
     
     Rigidbody rb;
     Collider parentColl;
 
     public GameObject explosion;
 
     float speed = 100f;
     // Use this for initialization
     void Start ()
     {
 
         Debug.Log(transform.parent.gameObject.name);
         rb = GetComponent<Rigidbody>();
         parentColl = transform.parent.GetComponent<Collider>();
         rb.velocity = GetComponentInParent<Rigidbody>().velocity;
         StartCoroutine(Lifetime());
     }
     void FixedUpdate()
     {
         Move();
     }
 
     void Move()
     {
         rb.AddRelativeForce(Vector3.forward * speed);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other != parentColl)
         {
             Instantiate(explosion, transform.position, transform.rotation);
             Destroy(this.gameObject);
         }
     }
 
     IEnumerator Lifetime()
     {
         yield return new WaitForSeconds(3f);
         Instantiate(explosion, transform.position, transform.rotation);
         Destroy(this.gameObject);
     }
 
 }
 

Can anyone help me figure out what's going on here?

Comment
Add comment · Show 1
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 mmorro1222 · Dec 14, 2016 at 03:25 AM 0
Share

I've changed the start function on the torpedo to awake and it seems to have fixed it. I guess the ontriggerenter was managing to get called before start had a chance to set parentcoll

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Instantiated GameObject gets spawned as a child 2 Answers

Instantiate prefab as Child to remove and re-do later 2 Answers

Play parent animation without affecting the child animation 1 Answer

Instantiate object as child of object in scene c Sharp 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