Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Vardius · Feb 18, 2013 at 08:37 PM · c#linedraw

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)

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 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.

Comment
Add comment · 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

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

9 People are following this question.

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

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


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