Instantiate LineRenderer, each time an object hits a Plane

This is a continuation of the following thread, but thought I should create a new one as it’s a slightly different topic: Draw on Cube/Plane/2D Surface using Setpixel? - Unity Engine - Unity Discussions

Currently, whenever the raycast hits the plane, it creates a line, but when the user hits the plane again, it just carries on using the same line renderer. But i want to create a new line renderer each time the ray cast hits the plane.

My reason for doing this is because i want to change the colours of new lines, and when i change the colour of the line at run-time, it changes the whole line to the new colour.

To visalise what’s happening, pretend that the thick part of the line indicates the colour change, so if i had a line it would look like this:


and when i change the colour, it changes the whole colour of the line to this:

_____________________________________________________

What i want though is this to happen:

__________________________****

I know i have to instantiate a new object from a prefab, but the code listed in the thread, which i will also insert below this paragraph, it would still apply to the original line renderer? I want the code to only apply to the newest line renderer game object thats being instantiated.

    Texture2D tex = rend.material.mainTexture as Texture2D;
        Vector2 pixelUV = hit.textureCoord;

        list.Add ( new Vector3(pixelUV.x,pixelUV.y));

        if (lr == null) lr = GameObject.Find("LineRenderer").GetComponent<LineRenderer>();



        lr.numPositions = list.Count;
        lr.SetPositions (list.ToArray());

        //lr.loop = true; //the loop apparently doesn't exist in lr so it throws an error, which is why i commented it out
        lr.useWorldSpace = true;
        //lr.widthMultiplier = thickness;

My instinct is saying that the most efficient way is to somehow find how to target the new instantiated game object, but i don’t think thats possible, is it?

I would simply add a new component LineRenderer or in case my memory is bad and you cannot do that, then yes, a new object with a new line renderer to which you’ll add your new lines (positions).
I actually made a small goofy app like this once (totally different than yours, but same in the sense of multiple line renderers). :slight_smile:

I thought of that, but research, is saying that it’s not possible to add a component of same type to the same GameObject

That’s very good to hear that you have experience in this :slight_smile:

I just need to figure out how to target the newest game object. i was thinking maybe stored reference to the instantiated objects in a list, and then getting the latest one by calling the ‘list.Count - 1’, but i don’t know if that’s a good idea or not.

um… well, in case i’m not totally understanding what you want to do…
Let’s just check: they can draw whatever/as much as they want, but they must do something to ‘change’ their colour (for instance). So, when they restart, they may or may not be at the same spot they last left - is that right?

As for targetting the correct object, you can store the inUseLineRenderer in a static variable, perhaps. That’s probably what I would do.
There’s no need to keep a list of all of them, (unless you have some other unmentioned actions that you want to perform on them at some later time).
Each new LR would start at 0 positions (or 1, position 0… etc).

Yes, you are right. For debugging purposes, whenever i press a keyboard key, it changes colour, but in the final product, i’d have an object which i’d touch with my finger, which would change the colour.

I can’t find any documentation for that method on google…Oh :hushed:, unless you mean store the newest instantiated object in a variable, and then apply the code from the previous thread to that variable?

oh, yeah it’s not a method… I mean… Ya, say your last thread is dealing with all the details… When you first start “drawing” or what not, you’d simply assign: static CurrentLR = GetComponent();

right, then when you switch, you just re-assign to the latest one. But you adapt your code, so instead of calling whatever variable you were using previously, you can now use CurrentLR

I think that makes sense.

Yeah that makes perfect sense, thank you! it’s simple when you know how :slight_smile:

I have one more question:

    if (hit.collider.name == "Plane") {
            CurrentLR = new GameObject().AddComponent("LineRenderer") as LineRenderer;
        }

        lr = CurrentLR;

        lr.numPositions = list.Count;
        lr.SetPositions (list.ToArray());

The code above, i know its not a good way of when to instantiate the GameObject, as it will keep creating objects as long as the raycast is hitting the plane. Plus this code is in the update method…

Is there a way i can make sure than it only assigns it once when the raycast hits the plane, and then only enable that if statement to return true when the raycast stops hitting that plane?

and i just realised while writing this, that a new list will have to be created aswell, otherwise it will copy the same positions from the first line renderer.

Yes, you definitely need to reset the list… As for hitting the plane and what not, I admit that I don’t feel I have a complete understanding of what you’re describing. Before, I had thought that you’d have an option for the user to press to indicate that they want to change the brush or whatever… To me, that seemed like the ideal place to create a new game object and add a LR.
Obviously, you want to fix that up, and the concept is simple, but I’m not sure about how you’re describing it… Let me know if anything helped in that paragraph lol

Oh that makes sense, just clear the list as the contents of that list have already been added to the line renderer.

It’s hard to describe, imagine real life painting with a paintbrush, you paint a line on a canvas(plane), then you remove the paintbrush(raycast) from the canvas(plane) so that its no longer in contact. Then you dip the paintbrush(raycast) in another colour, then you touch the paintbrush(raycast) on the canvas(plane) again and paint another line.

Hmmm thats a good idea, so when the user changes his colour, then at the same time, i can create a new LineRenderer there…

Okay, that actually makes perfect sense as you described it… In fact, I was wondering if that’s what you meant, but was hesitant to assume.
So, all should be well for you going forward. It might even make more sense to create a new object when they lift off (or even rather place back down) on the drawing canvas. Because, that’s more natural. Otherwise, you could potentially connect line A to point B somewhere entirely new, but since their “brush” wasn’t on the canvas, they didn’t intend to draw [that]… err… lol.
Yes, maybe? :slight_smile:

Okay, its working now. So I made my original linerenderer gameobject a prefab, then i made the code instantiate that prefab whenever i change colour. Before when i lost contact with the plane, and then gained contact again, it would connect the point where i left contact with the point where i regained contact. But now, when i lose contact, change colour, then regain contact, it starts a new line :smile:

That’s what i was trying before you suggested to move the code to where i change the colour, but its long and hard to code this concept.

i dont understand this lol

okay, the last part that you didn’t understand. I mean let’s say i draw a line from the left side to the right side of my canvas. I then pick up my brush and move my head way over to the left, again. then i touch the canvas. In your modified code, if I haven’t changed the colour… have I now connected a line from the far right back to the far left?
That seems un-intuitive , since I didn’t “draw” it?. Maybe I’m misunderstanding.
The more we talk about this… I am happy for you at some points, then I’m like “Wow, hope I don’t tell him something horrible” :slight_smile:

Nothing wrong with just when you change colours… Maybe that’s okay/for the best.
I don’t know how complicated it is to change to that the Line Renderer is new after a lift and back down of the brush…

yes the line will be connected, because a new line is only created when i change colour.

yeah, to be honest, i’m happy with this, cause at least now my whole program is functional to an extent

How can i show you my appreciation for your help? you and starmanta have done wonders for me :smile:

Can’t speak for StarManta, but there’s no need for anything… Saying thanks was plenty :wink: haha.
Happy I could help, and wish you all the best with your program. Sounds pretty cool.

1 Like

Thank you! :smile:

No problem :slight_smile: