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)