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 *= 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(); *
-
}*
- }*
}