Quantcast
Channel: Questions in topic: "error"
Viewing all articles
Browse latest Browse all 7934

Instantiating Object Then Setting Parent Throws Error

$
0
0
Hi There, I Am Trying To Instantiate An Object And Then Set Its Parent To Another Object. When I Do So, I Get This Error : Can't destroy Transform component of 'XR-92(Clone)'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed. I Can Instantiate The Parent Object Fine But When Trying To Instantiate It And Set Its Parent It Throws That Error. using UnityEngine; using System.Collections; public class ComponentButton : MonoBehaviour { public GameObject ComponentObject; public string ItemType; public GameObject StockLock, ScopeLock, MagazineLock, GripLock, HandleLock, BarrelLock, AttachmentLock, MuzzleLock; public WeaponBuilder WB; void Start() { WB = GameObject.FindGameObjectWithTag("Gun Area").GetComponent(); } void Update() { StockLock = GameObject.Find("Stock Lock"); ScopeLock = GameObject.Find("Scope Lock"); MagazineLock = GameObject.Find("Magazine Lock"); GripLock = GameObject.Find("Grip Lock"); BarrelLock = GameObject.Find("Barrel Lock"); AttachmentLock = GameObject.Find("Attachment Lock"); MuzzleLock = GameObject.Find("Muzzle Lock"); HandleLock = GameObject.Find("Handle Lock"); } public void Build() { if(ItemType == "Receiver") { BuildReceiver(); } if (ItemType == "Stock") { BuildStock(); } } private void BuildStock() { var Item = Instantiate(ComponentObject) as GameObject; Item.transform.parent = StockLock.transform; Item.transform.localScale = new Vector3 (1, 1, 1); } public void BuildReceiver() { var Item = Instantiate(ComponentObject) as GameObject; Item.transform.localPosition = GameObject.Find("Gun Area").transform.position; Item.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f); } }

Viewing all articles
Browse latest Browse all 7934

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>