Search Unity

Prefab loses scene variables

Discussion in 'Editor & General Support' started by diegoaespitia, Mar 11, 2019.

  1. diegoaespitia

    diegoaespitia

    Joined:
    Mar 3, 2019
    Posts:
    1
    Hello. So I created a Bomberman character. I instantiate a prefab bomb from him. Which I can toss to an empty game object that I labeled as the target. However, when the bomb is instantiated it loses the variable for the target because the bomb is a prefab and the target is a child of the player in the scene. I'm having lots of trouble trying to give the bomb a target upon instantiation. Is there any way to give the bomb a target either before it is created of immediately after so it can be thrown right away?

    Bomb.png


    My code to instantiate the bomb
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BombToss : MonoBehaviour
    6. {
    7.     public GameObject bomb;
    8.     public Transform hands;
    9.     public Player player;
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.GetButtonDown("Fire1"))
    14.         {
    15.             Instantiate(bomb, hands.position, Quaternion.Euler(-90, 0, 0));
    16.         }
    17.  
    18.     }
    19. }

    And my code to calculate the trajectory towards the target.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bomb : MonoBehaviour
    6. {
    7.     public Rigidbody bomb;
    8.     public Transform target;
    9.  
    10.     public float h = 25;
    11.     public float gravity = -18;
    12.  
    13.     private void Start()
    14.     {
    15.         bomb.useGravity = false;
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (Input.GetKeyDown(KeyCode.Space))
    21.         {
    22.             Launch();
    23.         }
    24.     }
    25.  
    26.     void Launch()
    27.     {
    28.         Physics.gravity = Vector3.up * gravity;
    29.         bomb.useGravity = true;
    30.         bomb.velocity = CalculateLaunchVelocity();
    31.     }
    32.  
    33.     Vector3 CalculateLaunchVelocity()
    34.     {
    35.         float displacementY = target.position.y - bomb.position.y;
    36.         Vector3 displacementXZ = new Vector3(target.position.x - bomb.position.x, 0, target.position.z - bomb.position.z);
    37.  
    38.         Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * h);
    39.         Vector3 velocityXZ = displacementXZ / (Mathf.Sqrt(-2 * h / gravity) + Mathf.Sqrt((-2 * displacementY) / gravity));
    40.  
    41.         return velocityXZ + velocityY * -Mathf.Sign(gravity);
    42.     }
    43. }
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,630
    Yeah, you can't store scene references in a prefab because prefab is not actually in a scene. An instance of a prefab can be in a scene, but the prefab itself Is more like an asset in the project window.
     
  3. mrmills_

    mrmills_

    Joined:
    Aug 15, 2022
    Posts:
    3
    So what are you supposed to do instead?
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    You need to populate references to objects in the scene after you have instantated the prefab.

    In not-the-actual-code, that would look something like:
    Code (csharp):
    1.  
    2. public Transform bombTarget;
    3.  
    4. void DropBomb()
    5. {
    6.     // Make an instance of the Prefabbed object in the Scene
    7.     GameObject bombGO = Instantiate(bombPrefab);
    8.     bombGO.transform.position = this.transform.position;
    9.  
    10.     // Do any Scene-specific initialisation required
    11.     Bomb bombComponent = bombGO.GetComponent<Bomb>();
    12.     bombComponent.target = bombTarget;
    13. }
     
    mrmills_ likes this.