Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · Jun 16, 2016 at 12:39 AM · c#2dcolliderpointsedge

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ochreous · Jun 16, 2016 at 08:11 PM 0
Share

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;
         }
avatar image jkramar · Jun 16, 2016 at 08:25 PM 0
Share

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?

avatar image Ochreous jkramar · Jun 16, 2016 at 09:26 PM 0
Share

Except the 2D Edge Collider points would still be there when I re-enable it. I want the edgCldr2d.points to reset like lineRndr.

avatar image jkramar Ochreous · Jun 16, 2016 at 09:33 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges