- Home /
Drawning Line Problem useing custom class
hey i want to draw line useing custom class: using System; using UnityEngine;
public class Drawing
{
public static Texture2D lineTex;
public static void DrawLine(Rect rect) { DrawLine(rect, GUI.contentColor, 1.0f); }
public static void DrawLine(Rect rect, Color color) { DrawLine(rect, color, 1.0f); }
public static void DrawLine(Rect rect, float width) { DrawLine(rect, GUI.contentColor, width); }
public static void DrawLine(Rect rect, Color color, float width) { DrawLine(new Vector2(rect.x, rect.y), new Vector2(rect.x + rect.width, rect.y + rect.height), color, width); }
public static void DrawLine(Vector2 pointA, Vector2 pointB) { DrawLine(pointA, pointB, GUI.contentColor, 1.0f); }
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color) { DrawLine(pointA, pointB, color, 1.0f); }
public static void DrawLine(Vector2 pointA, Vector2 pointB, float width) { DrawLine(pointA, pointB, GUI.contentColor, width); }
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width)
{
Matrix4x4 matrix = GUI.matrix;
if (!lineTex) { lineTex = new Texture2D(1, 1); }
Color savedColor = GUI.color;
GUI.color = color;
float angle = Vector3.Angle(pointB - pointA, Vector2.right);
if (pointA.y > pointB.y) { angle = -angle; }
GUIUtility.ScaleAroundPivot(new Vector2((pointB - pointA).magnitude, width), new Vector2(pointA.x, pointA.y + 0.5f));
GUIUtility.RotateAroundPivot(angle, pointA);
GUI.DrawTexture(new Rect(pointA.x, pointA.y, 1, 1), lineTex);
GUI.matrix = matrix;
GUI.color = savedColor;
}
}
and when in my script i use
Drawing.DrawLine(_temp, sphere[i, j].transform.position, Color.black);
i got errors:
NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode scaleMode, Boolean alphaBlend, Single imageAspect) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUI.cs:155) UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUI.cs:152) Drawing.DrawLine (Vector2 pointA, Vector2 pointB, Color color, Single width) (at Assets/Drawing.cs:67) Drawing.DrawLine (Vector2 pointA, Vector2 pointB, Color color) (at Assets/Drawing.cs:30) Main.OnEnter (System.String name) (at Assets/Main.cs:123) UnityEngine.GameObject:SendMessage(String, Object) focus:OnMouseDown() (at Assets/focus.cs:7) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
Answer by robertbu · Feb 18, 2013 at 10:42 PM
Since you are using GUI calls, you will need to make these calls from inside an OnGUI() function. If you don't, you'll get a null reference like this one.
On another note: while it's not causing you null reference problem, GUI.DrawTexture() takes screen coordinates. It looks like you are passing world coordinates to DrawLine method, and I don't see any code making the conversion.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Draw line from one point to another using mouse 0 Answers
How would I replicate Gizmos.DrawLine in-game? 1 Answer
A node in a childnode? 1 Answer