Search Unity

spawned ball

Discussion in 'Scripting' started by healym, Jul 3, 2017.

  1. healym

    healym

    Joined:
    May 17, 2016
    Posts:
    19
    Hi,

    I've placed a script on a spawned object that doesn't work. I've used the script to detect colliders with a ball prefab from the hierarchy and it works perfectly, but when I try the same script on the same prefab that spawns in the game it doesn't recognize the colliders. The ball script detects the colliders by recognizing the collider's tag. I've tried using the prefab from the project window instead of the hierarchy for the spawned ball, but that doesn't work either. Is there a reason why the script wouldn't work on a spawned object?
     
  2. Khazmadu

    Khazmadu

    Joined:
    Jun 24, 2017
    Posts:
    92
    I don't understand your problem.
    How do you place a script on a spawned object? Do you attach the Script on runtime?

    I think you should post some parts of your code and maybe try to explain your problem different.
     
  3. healym

    healym

    Joined:
    May 17, 2016
    Posts:
    19
    Thanks for getting back to me, I'll try to explain it better. I've attached a script to a ball prefab in the assets folder that detects a collider (the specific tag associated with the collider). When the ball is spawned in the game it doesn't trigger the plane to tilt when the ball comes in contact with the collider. However, when I place the same script on a ball prefab from the Hierarchy it is able to detect the colliders and makes the plane tilt. Here is the detect script I'm using:

    Code (CSharp):
    1. public class DetectWall : MonoBehaviour {
    2.  
    3.     public GameObject Platform; //Level GameObject that gets rotated
    4.     private RotatePlane rPlane; //Level GameObject's script to access rotation coroutines
    5.     public GameObject CamAnchor; //The Empty Game Object the Camera is parented under, to be a pivot for rotation
    6.     private CamRotate camRot; //Camera Pivot's script to access rotation coroutines
    7.  
    8.     public float thrust; //To push the ball, how fast you move it
    9.     private Rigidbody rb; //The ball's rigidbody
    10.  
    11.     private Coroutine rotCoroutine; //platform rotation coroutine being used
    12.     private Coroutine camCoroutine; //camera rotation coroutine being used
    13.  
    14.     void Start()
    15.     {
    16.         rPlane = Platform.GetComponent<RotatePlane> (); //Get the script from the platform
    17.         camRot = CamAnchor.GetComponent<CamRotate>(); //Get the script from the cam Pivot/Anchor
    18.  
    19.         rb = GetComponent<Rigidbody> (); //Get the ball's rigidbody for pushing it manually
    20.     }
    21.  
    22.     void OnCollisionEnter(Collision col)
    23.     {
    24.         if(col.gameObject.tag == "TopWall") //If the collider is tagged as "TopWall"
    25.         {
    26.             if (rotCoroutine != null) { //Stops old platform rotation coroutine (if there is one). This is because the two seperate slerps at once can make it wonky
    27.                 StopCoroutine (rotCoroutine);
    28.             }
    29.             if (camCoroutine != null) { //Stops old camera rotation coroutine (if there is one)
    30.                 StopCoroutine (camCoroutine);
    31.             }
    32.             rotCoroutine = StartCoroutine (rPlane.TopWall ()); //Starts the plane's TopWall rotation coroutine.
    33.             camCoroutine = StartCoroutine (camRot.TopMove ()); //Starts the camera's TopRot rotation coroutine.
    34.         }
    35.         if(col.gameObject.tag == "BottomWall")
    36.         {
    37.             if (rotCoroutine != null) {
    38.                 StopCoroutine (rotCoroutine);
    39.             }
    40.             if (camCoroutine != null) {
    41.                 StopCoroutine (camCoroutine);
    42.             }
    43.             rotCoroutine = StartCoroutine (rPlane.BottomWall ());
    44.             camCoroutine = StartCoroutine (camRot.BottomMove ());
    45.         }
    46.         if(col.gameObject.tag == "LeftWall")
    47.         {
    48.             if (rotCoroutine != null) {
    49.                 StopCoroutine (rotCoroutine);
    50.             }
    51.             if (camCoroutine != null) {
    52.                 StopCoroutine (camCoroutine);
    53.             }
    54.             rotCoroutine = StartCoroutine (rPlane.LeftWall ());
    55.             camCoroutine = StartCoroutine (camRot.LeftMove ());
    56.         }
    57.         if(col.gameObject.tag == "RightWall")
    58.         {
    59.             if (rotCoroutine != null) {
    60.                 StopCoroutine (rotCoroutine);
    61.             }
    62.             if (camCoroutine != null) {
    63.                 StopCoroutine (camCoroutine);
    64.             }
    65.             rotCoroutine = StartCoroutine (rPlane.RightWall ());
    66.             camCoroutine = StartCoroutine (camRot.RightMove ());
    67.         }
    68.         if (col.gameObject.tag == "Flatten") {
    69.             if (rotCoroutine != null) {
    70.                 StopCoroutine (rotCoroutine);
    71.             }
    72.             if (camCoroutine != null) {
    73.                 StopCoroutine (camCoroutine);
    74.             }
    75.             rotCoroutine = StartCoroutine (rPlane.BaseWall ());
    76.             camCoroutine = StartCoroutine (camRot.EvenCamera ());
    77.         }
    78.     }
    79.     void OnTriggerEnter(Collider col)
    80.     {
    81.         if(col.gameObject.tag == "TopWall") //If the collider is tagged as "TopWall"
    82.         {
    83.             if (rotCoroutine != null) { //Stops old platform rotation coroutine (if there is one). This is because the two seperate slerps at once can make it wonky
    84.                 StopCoroutine (rotCoroutine);
    85.             }
    86.             if (camCoroutine != null) { //Stops old camera rotation coroutine (if there is one)
    87.                 StopCoroutine (camCoroutine);
    88.             }
    89.             rotCoroutine = StartCoroutine (rPlane.TopWall ()); //Starts the plane's TopWall rotation coroutine.
    90.             camCoroutine = StartCoroutine (camRot.TopMove ()); //Starts the camera's TopRot rotation coroutine.
    91.         }
    92.         if(col.gameObject.tag == "BottomWall")
    93.         {
    94.             if (rotCoroutine != null) {
    95.                 StopCoroutine (rotCoroutine);
    96.             }
    97.             if (camCoroutine != null) {
    98.                 StopCoroutine (camCoroutine);
    99.             }
    100.             rotCoroutine = StartCoroutine (rPlane.BottomWall ());
    101.             camCoroutine = StartCoroutine (camRot.BottomMove ());
    102.         }
    103.         if(col.gameObject.tag == "LeftWall")
    104.         {
    105.             if (rotCoroutine != null) {
    106.                 StopCoroutine (rotCoroutine);
    107.             }
    108.             if (camCoroutine != null) {
    109.                 StopCoroutine (camCoroutine);
    110.             }
    111.             rotCoroutine = StartCoroutine (rPlane.LeftWall ());
    112.             camCoroutine = StartCoroutine (camRot.LeftMove ());
    113.         }
    114.         if(col.gameObject.tag == "RightWall")
    115.         {
    116.             if (rotCoroutine != null) {
    117.                 StopCoroutine (rotCoroutine);
    118.             }
    119.             if (camCoroutine != null) {
    120.                 StopCoroutine (camCoroutine);
    121.             }
    122.             rotCoroutine = StartCoroutine (rPlane.RightWall ());
    123.             camCoroutine = StartCoroutine (camRot.RightMove ());
    124.         }
    125.         if (col.gameObject.tag == "Flatten") {
    126.             if (rotCoroutine != null) {
    127.                 StopCoroutine (rotCoroutine);
    128.             }
    129.             if (camCoroutine != null) {
    130.                 StopCoroutine (camCoroutine);
    131.             }
    132.             rotCoroutine = StartCoroutine (rPlane.BaseWall ());
    133.             camCoroutine = StartCoroutine (camRot.EvenCamera ());
    134.         }
    135.     }
    136.     void Update()
    137.     {
    138.         //Controls to move the ball (for testing)
    139.         if (Input.GetKey (KeyCode.W)) {
    140.             rb.AddForce (0, 0, thrust);
    141.         }
    142.         if (Input.GetKey (KeyCode.S)) {
    143.             rb.AddForce (0, 0, thrust * -1);
    144.         }
    145.         if (Input.GetKey (KeyCode.A)) {
    146.             rb.AddForce (thrust * -1, 0, 0);
    147.         }
    148.         if (Input.GetKey (KeyCode.D)) {
    149.             rb.AddForce (thrust, 0, 0);
    150.         }
    151.     }
    152.  
    153. }
     
  4. healym

    healym

    Joined:
    May 17, 2016
    Posts:
    19
    Here is the ball script that was added to it before I added my script (it's somebody else's javascript):

    Code (JavaScript):
    1. // Ball.js : Description : this script manage the ball (sound, collision, trail)
    2. #pragma strict
    3. private var rb                    : Rigidbody;                                    // Ball RigidBody Component
    4. private var trail                : TrailRenderer;                                // Ball TrailRenderer Component
    5. @Header ("The maximum speed of the ball")  
    6. var maxSpeed                    : float = 25;                                    // change the ball Max speed on table. If you change the cabinet scale you probably must increase the Max speed
    7.  
    8. @Header ("Trail")                                                      
    9. var Speed_To_Activate_Trail     : float = .5;                                    // The Trail become active when ball speed is superior to Speed_To_Activate_Trail
    10. private var b_trail             : boolean = true;
    11.  
    12. private var tmp_vel             : Vector3;                                        // Used with the function Ball_Pause()
    13.  
    14. @Header ("Roll Sound")  
    15. private var roll_audio            : AudioSource;                                    // roll AudioSource Component
    16. private var once                : boolean = false;                                // Boolean used to play roll sound only if the sound is not playing
    17. var min_Mag_roll_audio            : float = 1;                                    // The minimum speed to play the roll sound
    18. private var tmp_Save_Min_Mag    : float = 0;
    19.  
    20.  
    21.  
    22. var b_Shake                        : boolean = false;
    23. var Shake_Force                    : float = 2;
    24.  
    25.  
    26. private var b_OnHole            : boolean = false;                                // Use to know if ball is on a hole or not
    27.  
    28.  
    29. function Start() {                                                                // --> function Start          
    30.     rb = GetComponent.<Rigidbody>();                                            // Access <Rigidbody>() Component;  
    31.     trail = GetComponent.<TrailRenderer>();                                        // Access <TrailRenderer>() Component;  
    32.  
    33.  
    34.     roll_audio = GetComponent.<AudioSource>();                        // Access <AudioSource>() Component; if roll sound is selected on the inspector
    35.  
    36. }
    37.  
    38. function Ball_Shake(Direction : Vector3){
    39.     if(rb)rb.AddForce(Direction*Shake_Force, ForceMode.VelocityChange);  
    40. }
    41.  
    42.  
    43. function FixedUpdate()                                                            // --> Fixed Update : FixedUpdate is used to deal with Physics
    44. {
    45.     if(rb.velocity.magnitude > maxSpeed)                                        // Limit ball speed.
    46.     {
    47.         rb.velocity            
    48.             = rb.velocity.normalized * maxSpeed;
    49.     }
    50.  
    51.     if(b_trail && rb.velocity.magnitude > Speed_To_Activate_Trail)                // Enable ball trail.
    52.     {
    53.          trail.enabled=true;
    54.          b_trail = false;
    55.     }
    56.     if(!b_trail && rb.velocity.magnitude < Speed_To_Activate_Trail)                // Desable ball trail.
    57.     {
    58.          trail.enabled=false;
    59.          b_trail = true;
    60.     }
    61.  
    62.     if(rb.velocity.magnitude > min_Mag_roll_audio && once)                        // Play the roll sound.
    63.     {
    64.          roll_audio.Play();
    65.          once = false;
    66.     }
    67.     else if(rb.velocity.magnitude <= min_Mag_roll_audio && !once)                // Stop the roll sound.
    68.     {
    69.          roll_audio.Stop();
    70.          roll_audio.pitch = 1;
    71.          once = true;
    72.     }
    73.  
    74.     if(!once && tmp_Save_Min_Mag == 0){
    75.         roll_audio.pitch = rb.velocity.magnitude/2.5;                                // When ball accelerate the pitch increase.
    76.        }
    77.  
    78. }
    79.  
    80.  
    81. function Ball_Pause(){                                                            // --> Function Call When the game is on pause
    82.     if(!b_OnHole){
    83.         if(!rb.isKinematic){                                                    // Start Pause
    84.             tmp_vel = rb.velocity;
    85.             rb.isKinematic = true;
    86.         }
    87.         else{                                                                    // Stop Pause
    88.             rb.isKinematic = false;
    89.             rb.velocity = tmp_vel;
    90.         }  
    91.     }  
    92. }
    93.  
    94. function OnHole(){                                                                // --> Know if ball is on a hole. Prevent bug when a ball enter a hole and player press pause.
    95.     b_OnHole = true;
    96. }
    97.  
    98. function OutsideHole(){                                                          
    99.     b_OnHole = false;
    100. }
    101.  
    102.  
    103. function OnTriggerEnter(other : Collider){
    104.     if(other.transform.tag == "Ramp_Sound" && tmp_Save_Min_Mag == 0){
    105.         Debug.Log("Ramp SOund Start");
    106.         tmp_Save_Min_Mag = min_Mag_roll_audio;
    107.         min_Mag_roll_audio = 0;
    108.     }
    109.     else if(other.transform.tag == "Ramp_Sound"){
    110.         Debug.Log("Ramp SOund Stop ");
    111.         min_Mag_roll_audio = tmp_Save_Min_Mag;
    112.         tmp_Save_Min_Mag = 0;
    113.     }
    114. }
     
  5. healym

    healym

    Joined:
    May 17, 2016
    Posts:
    19
    Just bumping this up again. Does anybody have an idea of how to make this work?