• 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
0
Question by gonzalezc9999 · Mar 02, 2020 at 07:17 PM · prefabsprefab-instance

Can not instantite a prefab under a prefab

I am trying to make a prefab be instantiated so that it goes under the camera. The camera under the player prefab. Here is my code to instantiate: `using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PickupWeapon : MonoBehaviour { public GameObject myspawnpad; public GameObject gunIgive;

 private void OnTriggerStay(Collider other)
 {
     
     if(other.gameObject.tag == "Player")
     {
         Debug.Log("player entered");
         if(Input.GetButton("PickUpWeapon"))
         {
             var newgunIgive = gunIgive;
             Debug.Log("it works");
             GameObject playercam = other.gameObject.GetComponent<playercontroller>().mycam;
             bool doesplayerhaveagun = other.gameObject.GetComponentInChildren<playercontroller>().gunactive;
             if (doesplayerhaveagun ==true)
             {
                 GameObject playergun = other.gameObject.GetComponentInChildren<Gunshooting>().gun;
                 Instantiate(newgunIgive, playergun.transform.position, playergun.transform.rotation, playercam.transform);
                 //gunIgive.transform.parent = playercam.transform.parent;
                 other.gameObject.GetComponentInChildren<playercontroller>().gunactive = true;
                 Destroy(playergun);
             }
             if (doesplayerhaveagun == false)
             {
                 Debug.Log("I had no gun");
                 Instantiate(newgunIgive, playercam.transform.position, gunIgive.transform.rotation, playercam.transform);
                 //gunIgive.transform.parent = playercam.transform.parent;
                 other.gameObject.GetComponentInChildren<playercontroller>().gunactive = true;



             }
             

         }
     }
 }

}

here's my player controller if it should be helpful:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playercontroller : MonoBehaviour
 {
     public float speed = 10.0f;
     public float Hspeed = 5.0f;
     public float rotationSpeed = 100.0f;
     public float jumpspeed;
     public Rigidbody myrigidbody;
     public bool ontheground;
     public GameObject Gun;
     public bool gunactive;
     public GameObject Melee;
     public bool meleeactive;
     public GameObject mycam;
     private void Start()
     {
         myrigidbody = GetComponent<Rigidbody>();
         mycam = GetComponentInChildren<cameracontroller>().thiscamera;
        //The following code checks for weapons
             Melee = GetComponentInChildren<MeleeSwing>().meleeweapon;
         Melee.SetActive(false);
         gunactive = true;
         meleeactive = false;
     }
     void Update()
     {
        
             //Check if we pressed switch weapon
         if (Input.GetButtonDown("SwapWeapon"))
         {
             StartCoroutine(Swap);
         }
         //this checks wether or not we can jump.
         if(Input.GetButtonDown("Jump") && ontheground == true)
         {
             myrigidbody.AddForce(Vector3.up * jumpspeed);
             ontheground = false;
         }
         // Get the horizontal and vertical axis.
         // By default they are mapped to the arrow keys.
         // The value is in the range -1 to 1
         float translation = Input.GetAxis("Vertical") * speed;
         float Htranslation = Input.GetAxis("Horizontal") * Hspeed;
         float rotationpos = Input.GetAxis("Mouse X") * rotationSpeed;
 
         // Make it move 10 meters per second instead of 10 meters per frame...
         translation *= Time.deltaTime;
         Htranslation *= Time.deltaTime;
         rotationSpeed *= Time.deltaTime;
 
         // Move translation along the object's z-axis
         transform.Translate(0, 0, translation);
         transform.Translate(Htranslation, 0, 0);
 
         // Rotate around our y-axis
         transform.Rotate(0, rotationSpeed, 0);
     }
    
     private void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.tag == "ground")
         {
             ontheground = true;
         }
     }
     public IEnumerator Swap
     {
         get
         {
 
             Debug.Log("swapping weapons");
             //Checks if the gun is out instead of the melee
             if (gunactive == true && meleeactive == false)
             {
                 yield return new WaitForSeconds(3);
                 Gun.SetActive(false);
                 gunactive = false;
                 Melee.SetActive(true);
                 meleeactive = true;
                  yield return new WaitForSeconds(2);
             }
             
             else if(gunactive == false && meleeactive == true)
             {
                 Melee.SetActive(false);
                 meleeactive = false;
                 Gun.SetActive(true);
                 gunactive = true;
                 yield return new WaitForSeconds(2);
             }
            
         }
     }
 }
 

whenever I instantiate it, it doesn't go as a child under my camera which is what I put in the code but instead, it gets instantiated as a child to the player. I have no idea how to fix this.

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

0 Replies

· Add your reply
  • Sort: 

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

204 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

Related Questions

Problems with instantiating 1 Answer

Internal Unity Error when trying to save a Prefab. 1 Answer

Instantiate prefab on inspector drop 0 Answers

Unity overwrites prefab values in runtime 0 Answers

Randoms in prefabs 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