Search Unity

Im missing something here, more eyes might help

Discussion in 'Scripting' started by DusanPan, Dec 8, 2018.

  1. DusanPan

    DusanPan

    Joined:
    Dec 4, 2018
    Posts:
    2
    so i made this code, from a tutorial, but it wont work

    im getting these errors:
    error CS0103: The name `explosionRadius' does not exist in the current context
    error CS0103: The name `explosionForce' does not exist in the current context
    error CS0103: The name `explosionRadius' does not exist in the current context
    error CS0103: The name `explosionUpward' does not exist in the current context





    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Explosion : MonoBehaviour {
    public float cubeSize = 0.2f;
    public int cubesInRow = 5;
    float cubesPivotDistance;
    Vector3 cubesPivot;

    // Use this for initialization
    void Start () {
    //calculate pivot distance
    cubesPivotDistance = cubeSize * cubesInRow / 2;
    //use this value to create pivot vector
    cubesPivot = new Vector3(cubesPivotDistance, cubesPivotDistance, cubesPivotDistance);

    }
    // Update is called once per frame
    void Update() { }

    private void OnTriggerEnter(Collider other){
    if (other.gameObject.name == "Wall")
    {
    explode();
    }

    }
    public void explode()
    {
    //make object disapper
    gameObject.SetActive(false);

    //loop 3 times to create 5x5x5 pieces in x,y,z coordinates
    for (int x = 0; x < cubesInRow; x++)
    { for (int y = 0; y < cubesInRow; y++)
    { for (int z = 0; z < cubesInRow; z++)
    {
    createPiece(x, y, z);
    }
    }

    }

    //get expolosion position
    Vector3 explosionPos = transform.position;
    //get colliders in that position and radius
    Collider[] colliders = Physics.OverlapSphere(explosionPos, explosionRadius);
    //add explosion force to all colliders in that overlap sphere
    foreach (Collider hit in colliders)
    {
    //get rigidbody from collider object
    Rigidbody rb = hit.GetComponent<Rigidbody>();
    if (rb != null)
    {
    //add explosion force to this body with given parameters
    rb.AddExplosionForce(explosionForce, transform.position, explosionRadius, explosionUpward);
    }
    }
    }
    void createPiece(int x, int y, int z) {
    //create piece
    GameObject piece;
    piece = GameObject.CreatePrimitive(PrimitiveType.Cube);
    //create piece position and scale
    piece.transform.position = transform.position + new Vector3(cubeSize * x, cubeSize * y, cubeSize * z) - cubesPivot;
    piece.transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
    //add rigidbody and set mass
    piece.AddComponent<Rigidbody>();
    piece.GetComponent<Rigidbody>().mass = cubeSize;

    }
    }
     
  2. Deleted User

    Deleted User

    Guest

    Your script is incomprehensible if you do not use code tags; it needs indentation.
     
  3. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77
    Please put your code in proper brackets ([.code])

    Well, you did not initialize any of these variables, thus the error.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    upload_2018-12-8_11-37-23.png
     
  5. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    Well ... The first thing you're missing is a descriptive title for your post.

    The second thing you're missing is code tags to make your post readable.

    That seems to be about it... You can go ahead and mark this as "SOLVED"
    ;)
     
  6. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    You need to takes those functions like e.g. explode() out of your main Update() function.
     
  7. mayankela21

    mayankela21

    Joined:
    Jul 2, 2019
    Posts:
    9
    Copy this Code Because you just not writte float and,
    Not you see on video because they are cut video on edited, this
    is youtuber mistake

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Explosion : MonoBehaviour {
    5. public float cubeSize = 0.2f;
    6. public int cubesInRow = 5;
    7. float cubesPivotDistance;
    8. Vector3 cubesPivot;
    9.  
    10. public float explosionForce = 150.0f;
    11. public float explosionRadius = 2.0f;
    12. public float explosionUpward = 0.2f;
    13.  
    14.  
    15. // Use this for initialization
    16. void Start () {
    17. //calculate pivot distance
    18. cubesPivotDistance = cubeSize * cubesInRow / 2;
    19. //use this value to create pivot vector
    20. cubesPivot = new Vector3(cubesPivotDistance, cubesPivotDistance, cubesPivotDistance);
    21.  
    22. }
    23. // Update is called once per frame
    24. void Update() { }
    25.  
    26. private void OnTriggerEnter(Collider other){
    27. if (other.gameObject.name == "Wall")
    28. {
    29. explode();
    30. }
    31.  
    32. }
    33. public void explode()
    34. {
    35. //make object disapper
    36. gameObject.SetActive(false);
    37.  
    38. //loop 3 times to create 5x5x5 pieces in x,y,z coordinates
    39. for (int x = 0; x < cubesInRow; x++)
    40. { for (int y = 0; y < cubesInRow; y++)
    41. { for (int z = 0; z < cubesInRow; z++)
    42. {
    43. createPiece(x, y, z);
    44. }
    45. }
    46.  
    47. }
    48.  
    49. //get expolosion position
    50. Vector3 explosionPos = transform.position;
    51. //get colliders in that position and radius
    52. Collider[] colliders = Physics.OverlapSphere(explosionPos, explosionRadius);
    53. //add explosion force to all colliders in that overlap sphere
    54. foreach (Collider hit in colliders)
    55. {
    56. //get rigidbody from collider object
    57. Rigidbody rb = hit.GetComponent<Rigidbody>();
    58. if (rb != null)
    59. {
    60. //add explosion force to this body with given parameters
    61. rb.AddExplosionForce(explosionForce, transform.position, explosionRadius, explosionUpward);
    62. }
    63. }
    64. }
    65. void createPiece(int x, int y, int z) {
    66. //create piece
    67. GameObject piece;
    68. piece = GameObject.CreatePrimitive(PrimitiveType.Cube);
    69. //create piece position and scale
    70. piece.transform.position = transform.position + new Vector3(cubeSize * x, cubeSize * y, cubeSize * z) - cubesPivot;
    71. piece.transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
    72. //add rigidbody and set mass
    73. piece.AddComponent<Rigidbody>();
    74. piece.GetComponent<Rigidbody>().mass = cubeSize;
    75.  
    76. }
    77. }
    this is Splitting Effect on Destoying Effect/
    You exauctaclly Know this
     
    Last edited: Nov 14, 2019