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 /
avatar image
0
Question by eddyzy · Sep 09, 2013 at 01:41 PM · instantiateraycastparentchildren

How to make an object the child of another object through code?

i have 2 bullethole prefabs(plane + cutout texture) when i shoot them (instantiate) i want to make them the childen of the object it hits;

if i have a dynamic box for example i want the plane to move with the box.

How can i do that ?

 using UnityEngine;
 using System.Collections;
 
 public class RayShooting : MonoBehaviour 
 {
     public GameObject []bulletTex;
     private GameObject currentTex;
     public Transform crosshair;
     public GameObject aimCamera;
     public float rayRange=1000f;
     
     void Update () 
     {
         
         Ray ray = camera.ViewportPointToRay(crosshair.position);
         RaycastHit hit;
     
 
         if(Input.GetButton("Fire1")&&(Physics.Raycast(ray, out hit))&&hit.collider.gameObject.CompareTag("Ground"))
         {
             currentTex=bulletTex[Random.Range(0,2)];
             Instantiate(currentTex,hit.point,Quaternion.FromToRotation(Vector3.up,hit.normal));
             currentTex.transform.parent=hit.collider.gameObject.transform.parent;//doesn`t work
     
         }
             
     
     }
 }
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
4
Best Answer

Answer by ArkaneX · Sep 09, 2013 at 01:52 PM

You're trying to assign parent of the object you hit (and it's probably null) as a parent of your bullet hole. You have to change these lines:

 Instantiate(currentTex,hit.point,Quaternion.FromToRotation(Vector3.up,hit.normal));
 currentTex.transform.parent=hit.collider.gameObject.transform.parent;

to

 var currentTexInstance = (GameObject)Instantiate(currentTex,hit.point,Quaternion.FromToRotation(Vector3.up,hit.normal));
 currentTexInstance.transform.parent = hit.collider.gameObject.transform;

or just

 var currentTexInstance = (GameObject)Instantiate(currentTex,hit.point,Quaternion.FromToRotation(Vector3.up,hit.normal));
 currentTexInstance.transform.parent = hit.collider.transform;

EDIT: changed to assign parent of instance, not of the prefab itself.

Comment
Add comment · Show 5 · 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 eddyzy · Sep 09, 2013 at 02:01 PM 0
Share

ok i get an error setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.

my bullet textures are prefabs. is there a way around this?

avatar image ArkaneX · Sep 09, 2013 at 02:10 PM 0
Share

You're righ - I missed this problem. The answer is now updated.

avatar image eddyzy · Sep 09, 2013 at 02:15 PM 0
Share

ok super but c# how`s that?

 GameObject currentTexInstance =  (GameObject)Instantiate(currentTex,hit.point,Quaternion.FromToRotation(Vector3.up,hit.normal));
 currentTexInstance.transform.parent = hit.collider.transform;

??

and i`v got another error

Actor::update$$anonymous$$assFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh shapes! Please change mesh geometry or supply a tensor manually!

when i shoot the dynamic box

avatar image ArkaneX · Sep 09, 2013 at 02:29 PM 1
Share

That was C# syntax :) var is C# keyword as well, and denotes any type. You can check details on $$anonymous$$SDN page.

The second error you get is unrelated. I haven't encountered it, but if you google for update$$anonymous$$assFromShapes, you'll find a solution quickly.

avatar image eddyzy · Sep 09, 2013 at 02:49 PM 0
Share

thx man the error was caused by the interaction between the rigidbody of the box and the mesh colider of the bullethole

i fixed it by removing the mesh colliders of the bulletholes

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Problem with prefab parent and children objects that need to be instantiated multiple times. 1 Answer

Instantiating a group of 8 objects, or 8 individually? 1 Answer

Checking Child collisions through parents 1 Answer

Create Clones as children in loops? 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