Hi, I am trying to create a script to Render the edge of the Composite Collider with LineRender in game. It will give a nice visual border to my play area in my game. I use Playmaker for most things so i am a bit rusty when it comes to coding and thought for something so simple may be someone could just help me a bit.
This is the code I have so far but unity is giving me an error with the Composite Collider part. See error below code. The for loop should set up an array of points taken from the Composite Collider. Then I need to do a bit more with the LineRender to set linewidth and Color. But I can’t get past this error. I know I am way off but it will take a week or more for me to figure this out on my own. Please Help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallCompositeCollider : MonoBehaviour
{
public float startWidth = 10;
public float endWidth = 10;
public Color startColor;
void Start()
{
for (int i = 0; i < CompositeCollider2D.pathCount; i++)
{
Vector2[] points = new Vector2[CompositeCollider2D.GetPathPointCount(i)];
CompositeCollider2D.GetPath(i, points);
}
LineRenderer.SetPositions(points);
}
void Update()
{
}
}
The Error is
Assets/Scripts/WallCompositeCollider.cs(14,43): error CS0120: An object reference is required to access non-static member `UnityEngine.CompositeCollider2D.pathCount’
Any hints or help would be a appreciated
Regards
Brad
CompositeCollider2D is the class, and what you need is an instance of the class. To use a metaphor, what you’re doing is “What color is a car?”, when you need to be asking it, “What color is that car?”
The way you do this for components in Unity is to GetComponent, which will (when called without any other context) find an instance of the given component that’s attached to the same GameObject as the script:
CompositeCollider2D myCollider = GetComponent<CompositeCollider2D>();
for (int i = 0; i < myCollider.pathCount; i++)
Thanks, that fixed that error. Now it doesn’t recognize my vector2 array ‘points’. Anyway I will look into that more tomorrow or later.
Ok I think I am closer but now i stuck trying to convert a Vector2[ ] to a Vector3[ ]. I thought what I had below would work Ok but Unity has the error
NullReferenceException: Object reference not set to an instance of an object
WallCompositeCollider.Start () (at Assets/Scripts/WallCompositeCollider.cs:27)
Line 27 is Points = points ;
```csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallCompositeCollider : MonoBehaviour
{
public float startWidth = 2;
public float endWidth = 2;
public Color startColor;
public Color endColor;
Vector3 Points;
Vector2 points;
void Start()
{ CompositeCollider2D myCollider = GetComponent();
LineRenderer lineRenderer = GetComponent();
lineRenderer.material = new Material (Shader.Find (“Sprites/Default”));
lineRenderer.startColor = startColor;
lineRenderer.endColor = endColor;
for (int i = 0; i <myCollider.pathCount; i++)
{
Vector2[] points = new Vector2[myCollider.GetPathPointCount(i)];
myCollider.GetPath(i, points);
Points [i] = points [i]; //Convert to Vector3 Array
}
lineRenderer.SetPositions(Points);
}
void Update()
{
}
}
_```*_