How to create & change Edge collider from code?

Hi,

I am trying to make my 2d sprites tiles with manually adjusting the edge collider from the code.
The problem is that I cant seem to find anyway to set the points for 2d edge collider?

The edge collider is always in the same position and I tried to adjust it from the code without any results.
I am not very good at coding so I couldnt get any correct way to do it from the Scripting reference.

http://docs.unity3d.com/ScriptReference/EdgeCollider2D.html

http://docs.unity3d.com/ScriptReference/EdgeCollider2D-points.html

What I understood that Edge Collider has an Vector2 arraylist, where I could use accessors to Set the new points but I am doing something wrong.

Here is my code (note that I attached the collider manually before in the editor for the object):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Tile : MonoBehaviour {

	// lets make verticies for the edge collider 2d
	public List<Vector2> newVerticies = new List<Vector2>();
	private EdgeCollider2D col;


	// Use this for initialization
	void Awake() {
		col = GetComponent<EdgeCollider2D>();
		float x = transform.position.x;
		float y = transform.position.y;
		newVerticies.Add( new Vector2(x, y) );
		newVerticies.Add( new Vector2(x + 0.5f, y) );
		newVerticies.Add( new Vector2(x +0.5f, y +0.5f) );
		newVerticies.Add( new Vector2(x, y + 0.5f) );
		col.Reset();


	}

	void getPointsToDebug() {
		foreach (Vector2 piste in col.points) {
			Debug.Log(piste);
		}
	}
	void setPoints() {
            // These wont work...!
		col.points[0] = newVerticies[0]; 
		col.points[1] = newVerticies[1];
	}
	
	// Update is called once per frame
	void Update () {
		Debug.DrawLine(newVerticies[0], newVerticies[1], Color.cyan);
		Debug.DrawLine(newVerticies[1], newVerticies[2], Color.red);
		Debug.DrawLine(newVerticies[2], newVerticies[3], Color.blue);
		Debug.DrawLine(newVerticies[3], newVerticies[0], Color.magenta);
	
		col.points[1].Set(1f,1f); // wont work neither..

		getPointsToDebug();
		setPoints();

	}
}

[3]

Thanks for the help!

EdgeCollider2D points coordinates are given relative to gameobject coordinates origin, so you don’t need to add offset, when you create new points. Last point in array must be the same as first, in this case collider will be closed. Don’t call setPoints() function every frame, but only when it’s really needed.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Tile : MonoBehaviour {
	
	// lets make verticies for the edge collider 2d
	public List<Vector2> newVerticies = new List<Vector2>();
	private EdgeCollider2D col;
	
	
	// Use this for initialization
	void Awake() {
		col = GetComponent<EdgeCollider2D>();
		newVerticies.Add( new Vector2(-0.5f, -0.5f) );
		newVerticies.Add( new Vector2(0.5f, -0.5f) );
		newVerticies.Add( new Vector2(0.5f, 0.5f) );
		newVerticies.Add( new Vector2(-0.5f, 0.5f) );
		newVerticies.Add( new Vector2(-0.5f, -0.5f) );
	}
	
	void getPointsToDebug() {
		foreach (Vector2 piste in col.points) {
			Debug.Log(piste);
		}
	}
	void setPoints() {
		col.points = newVerticies.ToArray();
	}
	
	// Update is called once per frame
	void Update () {
		getPointsToDebug();
		setPoints();	
	}
}

31165-sprite.png