Search Unity

Resolved Prefab in scene works but Instantiated prefab does not

Discussion in 'VR' started by OriolSC, Nov 29, 2020.

  1. OriolSC

    OriolSC

    Joined:
    Oct 7, 2019
    Posts:
    9
    Basically what the title says.

    I am working on a VR Oculus Go game where you throw balls that a machine provides to the waiters. The problem is that the original five balls that are in the scene work as expected but all the following ones which are an instantiated prefab of the original balls do not work and I have no idea why.

    The balls originally had two scripts and I thought that were was some kind of problem with script calling each other and I decided to merge both in one script but the problem still persists. Original scene balls work but instantiated prefabs do not. The script in question is this one:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. //DEBUG
    7. using UnityEngine.UI;
    8.  
    9. public class TestScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler {
    10.     bool holding = false;
    11.     bool pickable = true;
    12.     bool thrown = false;
    13.     GravityScript gravityScript;
    14.     MachineScript machineScript;
    15.     Renderer render;
    16.     Rigidbody rigid;
    17.     Transform balls, pointer;
    18.    
    19.     //DEBUG
    20.     Text debug;
    21.  
    22.     void Start() {
    23.         //DEBUG
    24.         debug = GameObject.Find("/Canvas/Debug").GetComponent<Text>();
    25.  
    26.         gravityScript = gameObject.GetComponent<GravityScript>();
    27.         machineScript = GameObject.Find("/Machine/S_Machine_Palanca").GetComponent<MachineScript>();
    28.         render = gameObject.GetComponent<Renderer>();
    29.         rigid = gameObject.GetComponent<Rigidbody>();
    30.         balls = GameObject.Find("/Balls").GetComponent<Transform>();
    31.         pointer = GameObject.Find("/Pointer/OculusGoController").GetComponent<Transform>();
    32.     }
    33.  
    34.     void Update() {
    35.         if (holding) {
    36.             Vector3 position = pointer.position;
    37.             transform.position = position + pointer.transform.forward * 0.1f;
    38.         }
    39.     }
    40.  
    41.     void OnTriggerExit(Collider collider) {
    42.         if (collider.gameObject.name == "S_Barra") machineScript.UpdateCounter();
    43.     }
    44.  
    45.     void OnCollisionEnter(Collision collision) {
    46.         switch (collision.gameObject.tag) {
    47.             case "Machine":
    48.                 if (thrown) Destroy (gameObject);
    49.                 break;
    50.             case "Eddie":
    51.                 Destroy (gameObject);
    52.                 break;
    53.             case "Sam":
    54.                 Destroy (gameObject);
    55.                 break;
    56.             case "Untagged":
    57.                 Destroy (gameObject);
    58.                 break;
    59.         }
    60.     }
    61.  
    62.     public void OnPointerEnter(PointerEventData eventData) {
    63.         debug.text = "ON POINTER ENTER";
    64.         if (pickable && !holding) render.material.SetColor("_Color", Color.cyan);
    65.     }
    66.  
    67.     public void OnPointerExit(PointerEventData eventData) {
    68.         debug.text = "ON POINTER EXIT";
    69.         if (pickable && !holding) render.material.SetColor("_Color", Color.yellow);
    70.     }
    71.  
    72.     public void OnPointerDown(PointerEventData eventData) {
    73.         debug.text = "ON POINTER DOWN";
    74.         if (pickable) {
    75.             holding = true;
    76.             rigid.useGravity = false;
    77.             transform.parent = pointer;
    78.             debug.text = "HOLDING";
    79.             render.material.SetColor("_Color", Color.green);
    80.         }
    81.     }
    82.  
    83.     public void OnPointerUp(PointerEventData eventData) {
    84.         debug.text = "ON POINTER UP";
    85.         if (pickable) {
    86.             thrown = true;
    87.             pickable = false;
    88.             transform.parent = balls;
    89.             Vector3 direction = transform.position - pointer.position;
    90.             rigid.AddForce(direction.normalized * 300);
    91.             gravityScript.enabled = true;
    92.             holding = false;
    93.             debug.text = "THROWING";
    94.             render.material.SetColor("_Color", Color.yellow);
    95.         }
    96.     }
    97. }
    98.  
    I already tried to make a debug apk and enable development builds and all that, but the app instantly crashes or I get a permanent black screen.

    And a short video to show what is exactly happening.

     
  2. OriolSC

    OriolSC

    Joined:
    Oct 7, 2019
    Posts:
    9
    I managed it to solve it by getting an early reference to the Transform component of the pointer because for some unknown reason I cannot seem to get it after the scene has started. That is why the first five original balls work meanwhile the others do not.