Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple Problem

Discussion in 'Editor & General Support' started by BrentMichie, Oct 7, 2015.

  1. BrentMichie

    BrentMichie

    Joined:
    Sep 10, 2014
    Posts:
    32
    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<LineRenderer>();
    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.

    Help?
     
  2. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    21
    try adding this at the top of your start section.
    Code (CSharp):
    1. myLR = new LineRenderer();
    Also why do you have if(true) sitting there around your code blocks? There is no reason for this that I can see?
     
  3. BrentMichie

    BrentMichie

    Joined:
    Sep 10, 2014
    Posts:
    32
    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.
     
    Last edited: Oct 7, 2015
  4. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    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.
     
    BrentMichie likes this.
  5. BrentMichie

    BrentMichie

    Joined:
    Sep 10, 2014
    Posts:
    32
    My apologies, new to this site. I will change it to Awake. I'm fairly certain it is being called because the other variables do change.

    Edit: It worked. Simply changed from Start to Awake. Now the lines aren't in the right place but I'm sure that's an easier fix. Appreciate it.

    Edit2: How do I put your answer as the correct answer Steve?