Hello,
Any help is greatly appreciated! I have a game object that adds the transforms of other objects into a list once they enter its collider. What I need to do (and have failed to do) is add a LineRenderer component for each transform in my list, set some variables of each LineRenderer that has been created, and then set the position (id 1) to the Transforms in my list. Basically when other objects get close I want to draw lines from the gameObject to those other objects. Here’s my code so far…
void Update()
{
//for each transfrom in transformList add a line renderer
for (int i = 0; i < transformList.Count; i++)
{
//I'm confused here: I don't know how to set variables of...
//the component I'm adding.
LineRenderer l = gameObject.AddComponent (typeof(LineRenderer)) as LineRenderer;
l.positionCount = 2;
l.SetPosition (0, this.gameObject.transform.position);
l.widthMultiplier = .0025f;
}
if (tractorActive == false && inCollider == true)
{
for (int i = 0; i < transformList.Count; i++)
{
//foreach line renderer on the game object, set its position (id 1) to the...
//transforms on my list.
LineRenderer[] lr = gameObject.GetComponents<LineRenderer>();
foreach (LineRenderer r in lr)
r.SetPosition (1, transformList [i].position);
}
}
}
I’m getting errors on lines 10 - 12 and the related question is how do I set the variables of an added component when I add it? Secondly, I was wondering if there’s a better way to go about this. Thanks in advance.
Edit: and now I’m seeing that I can’t add more than one Line Renderer to a game object. So yeah… happy to entertain any suggestions. I guess I could create child game objects with line renderers. Boo.
You’re real close. The code above is going to try to add all the line renderers to the gameObject this script is on, so that’s why it won’t work.
For one, line 9 above, you want to change out gameObject. to instead be:
transformList[i].AddComponent ... etc
I also prefer using the generic form of AddComponent:
LineRenderer l = transformList[i].AddComponent<LineRenderer>();
then in addition to setting position 0, you need to:
l.SetPosition( 1, transformList[i].position);
Try that, press pause, study your scene and see where the LRs are now.
Finally, I recommend NEVER using a lowercase L for your variables just because it’s too close to the digit 1.
Use either uppercase L or lr if you insist on being super-short. Ideally you would use lineRenderer (lowercase start) but I understand not wanting to do a lot of typing.
Thanks for your time and the advice! So I take it you’re suggesting that I add the line renderer component to each tranform’s game object in the list. I can see this working for some use cases. What I probably should have described better is that I need multiple lines emanating from single objects (imagine a node network). Each object has to draw lines to multiple game objects they come in contact with and vice versa.
The basic limitation in Unity is that a single GameObject can’t have more than one component of type Renderer, which is what LineRenderer is, and of course MeshRenderer, etc.
Therefore, the change I suggested puts it on each object.
A more generic solution would have a pool of GameObjects sitting around waiting to be employed as “GameObject hosting points,” and some kind of manager that would make more of them as needed (to a limit), and dole them out to anyone who wanted to make a line.
While we’re at it I’ll make a plug for an asset store package I bought and that I am VERY pleased with: Vectrosity.