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 Volatile_Potato · Jun 02, 2017 at 11:31 AM · arraylinerendererarray of gameobjects

Draw a line from gameobject at end of the array to the gameobject in the start of the array

Hi all,

I have got line renderer drawing a line through all game objects in an array. However, I would like to draw the line renderer from the game object at the end of the array back to the game object at the start of the array. All game objects that I am drawing a line through are instantiated objects. This code below draws a line renderer from game object at start of array to the game object stored at the end of the array. Any help is appreciated.

What I am doing so far:

 public void MeasureArea()
     {
         GameObject[] points = GameObject.FindGameObjectsWithTag("MeasuringPoint");
 
         lrA = GetComponent<LineRenderer>();
         lrA.material = new Material(Shader.Find("Transparent/Diffuse"));
         lrA.startWidth = 0.1f;
         lrA.endWidth = 0.1f;
         pointLine = new Vector3[points.Length];
 
 
         int n = points.Length;
 
 
         for (int i = 0; i < n - 1; i++)
         {
             pointLine[i] = points[i % n].transform.position;
             Debug.Log(points.Length);
         }
 
         lrA.numPositions = pointLine.Length;
         lrA.SetPositions(pointLine);
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
1

Answer by HarshadK · Jun 02, 2017 at 11:40 AM

You need to pass your first gameobject as the last item in your array.

 public void MeasureArea()
 {
     GameObject[] points = GameObject.FindGameObjectsWithTag("MeasuringPoint");
 
     lrA = GetComponent<LineRenderer>();
     lrA.material = new Material(Shader.Find("Transparent/Diffuse"));
     lrA.startWidth = 0.1f;
     lrA.endWidth = 0.1f;
     
     int n = points.Length + 1;
     pointLine = new Vector3[n];
 
     for (int i = 0; i < n - 1; i++)
     {
         pointLine[i] = points[i].transform.position;
         Debug.Log(points.Length);
     }
     
     // Just push first element at the last position.
     pointLine[n-1] = points[0];
 
     lrA.numPositions = n;
     lrA.SetPositions(pointLine);
 }
Comment
Add comment · Show 5 · 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 Volatile_Potato · Jun 02, 2017 at 12:00 PM 0
Share

Thanks for input, however when I instantiate 4 points (game objects) only 3 points connect as required but the 4th point is all on its own with no lines. :S

avatar image Volatile_Potato · Jun 02, 2017 at 01:13 PM 0
Share

@Harshad$$anonymous$$ This worked however I have an extra game object that isn't connected with the lines. e.g I placed 4 instantiated objects in the scene, but only lines are drawn from 3 points making a triangle. I am looking to draw lines between all game objects into the array, when at end of array it will draw a line to the gameobject at the start of the array. This was good but as mentioned above it left out a game object to form a triangle ins$$anonymous$$d of a square.

I made a change in code pointLine[n-1] = points[0]; to pointLine[n - 1] = pointLine[0] as it gave a compiler error " Cannot implicitly convert type 'UnityEngine.GameObject' to UnityEngine.Vector3'.

avatar image HarshadK Volatile_Potato · Jun 02, 2017 at 01:55 PM 0
Share

Change the line from my code:

 pointLine[n-1] = points[0];

to

 pointLine[n-1] = points[0].transform.position;


avatar image Volatile_Potato HarshadK · Jun 02, 2017 at 02:24 PM 0
Share

@Harshad$$anonymous$$ Thanks again for revisiting. I have made the changes as suggested, however I still get the same behaviour. When 4 points are instantiated the line only goes through 3 of the points to form a complete triangle leaving the fourth point out.

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

73 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

Related Questions

Missiles move to already destroy objects [Problem][Update] 4 Answers

How to randomly select multiple gameobjects from array? 1 Answer

How do I change gameObject values in an array all at once? 1 Answer

Wrong object always Instantiated 1 Answer

How to make an array list set to false gradually instead of instantly? 2 Answers


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