Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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:
    1
    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. AnneSchmidt

    AnneSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    1,160
    Your script is incomprehensible if you do not use code tags; it needs indentation.
     
  3. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    47
    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:
    2,488
    upload_2018-12-8_11-37-23.png
     
  5. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    237
    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,206
    You need to takes those functions like e.g. explode() out of your main Update() function.