- Home /
Question by
dendens2 · Apr 12, 2013 at 01:41 AM ·
javascript
How do I access variables of a component?
Hi, I am trying to access variables of a Line Renderer component, specificly the Element 0 and Element 1. How do I do that? I tried this but it did not work.
var tracerClone: GameObject;
tracer = Instantiate(tracer, Vector3(0, 0, 0), Quaternion.Euler(0, 0, 0));
tracerClone.GetComponent(LineRenderer).Positions.element0 = Vector3(hit.point);
Comment
Try this
var lineR : LineRenderer;
lineR = tracer.GetComponent(LineRenderer);
lineR.SetPosition(0,Vector3(hit.point));
Thanks, I marked the other one as the answer because he answered first though.
Best Answer
Answer by justinpatterson · Apr 12, 2013 at 01:58 AM
Instead of doing Positions.etc do SetPosition(positionNum, positionVal). This creates the position (doing Positions.etc would imply that the position already exists, I think).
gameObject.GetComponent(LineRenderer).SetPosition(0, gameObject.transform.position);
would create a new position in the array at array number 0. The next point to draw a line renderer to would be "...SetPosition(1, position)" and so on.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100143im_/https://answers.unity.com/themes/thub/images/avi.jpg)