Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trouble with assigning/generating Physics material

Discussion in 'Scripting' started by Richop, May 26, 2016.

  1. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136
    I've been trying to work out how to attach or set the physics materials bounciness/friction, I know it's this: gameObject.GetComponent<BoxCollider2D> ().sharedMaterial.bounciness = 1f;

    But I keep getting an error, "object reference not set to an instance of an object" and I don't quite know why, is it to do with not assigning a Physics material to the object and trying to set something that's not there? I don't really know how to assign a physics material to the collider when doing it generatively.

    Has anyone got any ideas?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class GenerateColliders : MonoBehaviour
    6. {
    7.  
    8.     public float widthOfCollider = 2f;
    9.     public float z_axis = 0f;
    10.     private Vector2 screenSize;
    11.  
    12.     void Start ()
    13.     {
    14.         Dictionary<string,Transform> colliders = new Dictionary<string,Transform>();
    15.         //Create GameObjects and add their Transform components to the Dictionary created above
    16.         colliders.Add("Top",new GameObject().transform);
    17.         colliders.Add("Bottom",new GameObject().transform);
    18.         colliders.Add("Right",new GameObject().transform);
    19.         colliders.Add("Left",new GameObject().transform);
    20.  
    21.         Vector3 cameraPos = Camera.main.transform.position;
    22.         screenSize.x = Vector2.Distance (Camera.main.ScreenToWorldPoint(new Vector2(0,0)),Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0))) * 0.5f;
    23.         screenSize.y = Vector2.Distance (Camera.main.ScreenToWorldPoint(new Vector2(0,0)),Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height))) * 0.5f;
    24.  
    25.                     //loop through the transforms in colliders
    26.             foreach(KeyValuePair<string,Transform> bc in colliders)
    27.             {
    28.                        //Add the collider component
    29.                     bc.Value.gameObject.AddComponent<BoxCollider2D>();
    30.  
    31.                     bc.Value.gameObject.GetComponent<BoxCollider2D> ().sharedMaterial.bounciness = 1f;
    32.  
    33.                     bc.Value.gameObject.GetComponent<BoxCollider2D> ().sharedMaterial.friction = 1f;
    34.  
    35.                     //give the objects a name
    36.                     bc.Value.name = bc.Key + "Collider";
    37.                       //Make the object with collider child of the object this script is attached to
    38.                     bc.Value.parent = transform;
    39.                        //Scale the object to the width and height of the screen
    40.                     if(bc.Key == "Left" || bc.Key == "Right")
    41.                         bc.Value.localScale = new Vector3(widthOfCollider, screenSize.y * 2, widthOfCollider);
    42.                     else
    43.                         bc.Value.localScale = new Vector3(screenSize.x * 2, widthOfCollider, widthOfCollider);
    44.             }
    45.                 colliders["Right"].position = new Vector3(cameraPos.x + screenSize.x + (colliders["Right"].localScale.x * 0.5f), cameraPos.y, z_axis);
    46.             colliders["Left"].position = new Vector3(cameraPos.x - screenSize.x - (colliders["Left"].localScale.x * 0.5f), cameraPos.y, z_axis);
    47.         colliders["Top"].position = new Vector3(cameraPos.x, cameraPos.y + screenSize.y + (colliders["Top"].localScale.y * 0.5f), z_axis);
    48.         colliders["Bottom"].position = new Vector3(cameraPos.x, cameraPos.y - screenSize.y - (colliders["Bottom"].localScale.y * 0.5f), z_axis);
    49.     }
    50. }
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
  3. Richop

    Richop

    Joined:
    Mar 18, 2016
    Posts:
    136
    Thanks I was just being dumb! I worked it out, was just coming back on to say!

    public physicsmaterial2d mat;
    and then shared material = mat;

    Gets late and sometimes the simplest things mess me up!
     
    ThermalFusion likes this.