Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can i unpack a prefab completely through a script

Discussion in 'Scripting' started by S3Critsss, Jun 22, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I tried to instantiate a prefab, set it as a child of an object then reset the position so its in the middle of that object but i got this error

    Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: 'Cyclopse').
    UnityEngine.Transform:SetParent(Transform)
    PlayerControls:Start() (at Assets/Scripts/PlayerScripts/PlayerControls.cs:50)


    at line 50 i do this


    chosenFace.transform.SetParent(FaceHolder.transform);
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    This usually happens when you try to set the parent of the original prefab rather than the instantiated copy.

    Post the full script if you're still struggling to resolve this.
     
  3. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using System.Diagnostics;
    6.  
    7. public class PlayerControls : MonoBehaviour
    8. {
    9.  
    10.     public GameObject pauseMenu;
    11.     public GameObject SpeedBoostText;
    12.     public GameObject SpeedBoostSFX;
    13.  
    14.     public float moveSpeed = 5f;
    15.  
    16.     int TotalCoins;
    17.     public int coins = 0;
    18.     public TMP_Text coinsText;
    19.     public TMP_Text HighCoinsText;
    20.  
    21.     bool isSpeeding = false;
    22.     float TimeBetweenSpeedBoost = 20f;
    23.     float TimeTillBoostEnds = 5f;
    24.     float reducingTime = 1.0f;
    25.  
    26.     float timer;
    27.     float minutes;
    28.     float seconds;
    29.     float hours;
    30.  
    31.     [SerializeField] TMP_Text stopWatchText;
    32.     [SerializeField] TMP_Text timePlayedText;
    33.  
    34.     Rigidbody2D rb;
    35.  
    36.     CameraShake cameraShake;
    37.  
    38.     public GameObject chosenFace;
    39.     public GameObject currentFace;
    40.     public GameObject FaceHolder;
    41.  
    42.     private void Start()
    43.     {
    44.         rb = this.GetComponent<Rigidbody2D>();
    45.         TotalCoins = PlayerPrefs.GetInt("Coins", 0);
    46.         timer = 0;
    47.         if (PlayerPrefs.GetString("chosenFace") != null) {
    48.             Instantiate(chosenFace, currentFace.transform.position, currentFace.transform.rotation);
    49.            
    50.             chosenFace.transform.SetParent(FaceHolder.transform);
    51.             chosenFace.transform.position = new Vector3(0,0,0);
    52.             chosenFace = currentFace;
    53.         }
    54.     }
    55.  
    56.     void StopWatchCalculator()
    57.     {
    58.         timer += Time.deltaTime;
    59.         seconds = (int)(timer % 60);
    60.         minutes = ((int)(timer / 60) % 60);
    61.         hours = (int)(timer / 3600);
    62.  
    63.         stopWatchText.text = hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00");
    64.         timePlayedText.text = "Time Played = " + hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00");
    65.     }
    66.  
    67.     void Update()
    68.     {
    69.         //StopWatch
    70.  
    71.         StopWatchCalculator();
    72.  
    73.         //Coins
    74.  
    75.         coinsText.text = "$ gained = " + coins.ToString();
    76.         PlayerPrefs.SetInt("Coins", TotalCoins + coins);
    77.         HighCoinsText.text = "Total $ = " + PlayerPrefs.GetInt("Coins", 0);
    78.         PlayerPrefs.Save();
    79.  
    80.         //Pausing
    81.         var cam = Camera.main;
    82.         if (Input.GetKeyDown(KeyCode.Escape))
    83.         {
    84.             Time.timeScale = 0f;
    85.             cam.GetComponent<AudioSource>().Stop();
    86.             pauseMenu.SetActive(true);
    87.             cameraShake.shouldShake = false;
    88.         }
    89.  
    90.         //SpeedBoost
    91.         TimeBetweenSpeedBoost -= reducingTime * Time.deltaTime;
    92.         if (TimeBetweenSpeedBoost <= 0f)
    93.         {
    94.             isSpeeding = true;
    95.             if (isSpeeding)
    96.             {
    97.                 Instantiate(SpeedBoostSFX, transform.position, transform.rotation);
    98.                 SpeedBoostText.SetActive(true);
    99.                 moveSpeed = 14f;
    100.                 TimeTillBoostEnds -= reducingTime * Time.deltaTime;
    101.                 if (TimeTillBoostEnds <= 0f)
    102.                 {
    103.                     isSpeeding = false;
    104.                     SpeedBoostText.SetActive(false);
    105.                     moveSpeed = 7f;
    106.                     TimeBetweenSpeedBoost = 20f;
    107.                     TimeTillBoostEnds = 5f;
    108.                 }
    109.             }
    110.         }
    111.     }
    112.  
    113.     private void FixedUpdate()
    114.     {
    115.         //Player Faces Mouse
    116.         var cam = Camera.main;
    117.         Vector3 mousePos = Input.mousePosition;
    118.         mousePos = cam.ScreenToWorldPoint(mousePos);
    119.         Vector2 direction = new Vector2(mousePos.x - transform.position.x,mousePos.y - transform.position.y);
    120.         transform.up = direction;
    121.  
    122.         //Player Moves Towards Mouse
    123.         direction.Normalize();
    124.         rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    125.     }
    126.  
    127.     void OnCollisionEnter2D(Collision2D col)
    128.     {
    129.         if (col.collider.tag == "Enemy")
    130.         {
    131.             coins += 1;
    132.         }
    133.     }
    134.  
    135. }
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    On lines 48 and 97 you are instantiating new objects, but you aren't saving a reference to the newly instantiated object, instead you are working with the original prefab.

    If you change it to this it shouldn't give you the error, notice where the new object reference is cached, so you can set its parent transform.
    Code (CSharp):
    1.         if (PlayerPrefs.GetString("chosenFace") != null) {
    2.             var newChosenFace = Instantiate(chosenFace, currentFace.transform.position, currentFace.transform.rotation);
    3.          
    4.             newChosenFace.transform.SetParent(FaceHolder.transform);
    5.             newChosenFace.transform.position = new Vector3(0,0,0);
    6.             chosenFace = currentFace;
    7.         }
    You also need to do the same thing at line 97 where you instantiate SpeedBoostSFX and again aren't caching a reference to the instantiated object.