- Home /
C# Invalid Points Assigned to 2D Edge Collider
I had this code working a moment ago and now I'm getting warnings saying that I've assigned invalid points to my 2D Edge Collider. I've tried checking if(keyVertices.Length > 0), if(keyVertices != null) and if(v != null) but none of them resolved this issue. I'm honestly not sure where this error is coming from.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BeamShooter : MonoBehaviour {
// Prefabs
public KeyCode fireKey;
public Transform point;
public Transform destination;
public LineRenderer lineRndr;
public EdgeCollider2D edgCldr2d;
// Timing
public float timeToUpdate = 0.05f;
public float timeToPowerUp = 0.5f;
public int maxNumVertices;
public int numVertices;
public float lastUpdate;
public AudioSource audSrc;
// Use this for initialization
void Awake()
{
lineRndr = GetComponent<LineRenderer>();
edgCldr2d = GetComponent<EdgeCollider2D>();
audSrc = GetComponent<AudioSource>();
numVertices = 0;
lastUpdate = 0.0f;
Check();
}
void Check(){
audSrc.volume = PlayerPrefs.GetFloat("SoundFXVolume");
fireKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Fire"));
}
// Update is called once per frame
void Update () {
if (Input.GetKey(fireKey)){
if (audSrc.pitch < 3.0f) {
audSrc.pitch += Time.deltaTime;
}
if (audSrc.pitch < 0.5f)
return;
if ((Time.time - lastUpdate) < timeToUpdate)
return;
lastUpdate = Time.time;
numVertices = Mathf.RoundToInt(Vector3.Distance(transform.position, point.position));
point.position = Vector3.MoveTowards(point.position, destination.position, 5 * Time.deltaTime * (1.0f / timeToPowerUp));
Vector3 currentV = transform.localPosition;
Vector2[] keyVertices = new Vector2[numVertices];
lineRndr.SetVertexCount(numVertices);
for (int i = 0; i < numVertices; i++) {
float vfl = (Random.value * 4.0f - 2.0f);
Vector3 v = new Vector2(currentV.x + vfl,currentV.y + vfl);
lineRndr.SetPosition(i, v);
//if(v != null)
keyVertices[i] = v;
currentV += (point.localPosition - transform.localPosition) / numVertices;
}
//if(keyVertices != null)
//if(keyVertices.Length != 0)
keyVertices[0] = transform.localPosition;
edgCldr2d.points = keyVertices;
lineRndr.SetPosition(0, transform.localPosition);
}
if (Input.GetKeyUp(fireKey)){
numVertices = 0;
lineRndr.SetVertexCount(0);
edgCldr2d.points = null;
point.position = transform.position;
audSrc.pitch = 0;
}
if(Time.timeScale == 0.0f){
Check();
}
}
}
Answer by jkramar · Jun 16, 2016 at 06:31 PM
Line 69, you can't set the array of points to null.
An edge collider must have at least 2 points.
I already tried edgCldr2d.points = new Vector2[]{new Vector2(0,0),new Vector2(0,0)}; ins$$anonymous$$d of edgCldr2d.points = null and I still kept getting the same error.
if (Input.Get$$anonymous$$eyUp(fire$$anonymous$$ey)){
numVertices = 0;
lineRndr.SetVertexCount(0);
edgCldr2d.points = new Vector2[]{new Vector2(0,0),new Vector2(0,0)};
point.position = transform.position;
audSrc.pitch = 0;
}
I can't test right now, but I suspect the fact that your points don't make sense for an edge collider is the problem.
Ins$$anonymous$$d of messing with the points, have you tried disabling the collider when you don't need it?
Except the 2D Edge Collider points would still be there when I re-enable it. I want the edgCldr2d.points to reset like lineRndr.
Only enable it once your points are set correctly.
Unless I'm mistaken (yiu didn't specify what line of code is giving the error), the code in the Input.Get$$anonymous$$ey block is working okay.
Your answer
Follow this Question
Related Questions
C# EdgeCollider2D Points Not Updating Continuously 0 Answers
C# Assigning EdgeCollider2D's Points 0 Answers
Multiple Cars not working 1 Answer
For Some reason, collider check has extra collider. 2 Answers
I can't get 2DCollider to work. 1 Answer