All I want to do is Instantiate a GameObject which contains a LineRenderer. Then I want to modify the Positions in this LineRenderer from the Script which Instantiated it. I have multiple prefabs with the same script on them for playermovement, this is where the Instantiation is started and where I want to pass the variables from (2x Vector3).
I have tried a bunch of stuff but I keep getting various errors. People have suggested stuff to me but nobody has posted the code which would do this.
Here is the LineCreator script which holds the code which setups up the LineRenderer:
public class LineCreator : MonoBehaviour {
public LineRenderer myLR;
// Use this for initialization
void Start () {
if (true) {
myLR = GetComponent();
myLR.SetWidth (0.1f, 0.1f);
myLR.SetPosition(0, Vector3.zero);
myLR.SetPosition(1, Vector3.zero);
myLR.material = new Material(Shader.Find(“Particles/Additive”));
if (true) {
myLR.SetColors (Color.white, Color.white);
}
}
}
public void SetPosition(int index, Vector3 pos) {
myLR.SetPosition (index, pos);
}
}
In the mover script I have code such as :
public LineCreator L;
public LineCreator R;
L = (LineCreator)Instantiate (R, Vector3.zero,Quaternion.identity);
L.myLR.SetPosition (0, firstPos);
L.myLR.SetPosition (1, secondPos);
The error I get from this is : The variable myLR of LineCreator has not been assigned.
You probably need to assign the myLR variable of the lineCreator script in the inspector.
I have put the LineCreator prefab onto the R variable slot in the inspector. Still doesn’t work.
I eventually want to add a condition there but for now I just want it to execute everytime. There are 2 scripts in question here. Should I add it to the playerMover script or the LineCreator?
Edit: I added it to the LineCreator because that seemed like the only logical place to add it and it didn’t do anything. If I put it in the playerMover script then I need to initialise a myLR variable in that script so I doubt that has anything to do with it.
First, you do not need myLR = new LineRenderer() as suggested above, since you’re already grabbing the component.
If the code in your mover is getting executed before the Start() initialization in LineCreator, it’s possible Start() hasn’t been called yet. You might try changing Start() to Awake(). Or you could create an initialize method that you explicitly call after instantiation.
On a side note, you should put code in between code tags so it’s formatted nicely.