Problem on referencing to a random variables value in C#

Hey there,

I have the following problem: I want to create a script (that itself is attached to a Gameobject called Ground) that spawns two objects. Each of these objects is attached with a LineRenderer of different height relative to the Ground. I want the position.y of the objects Line to be equal to the Y-Value of the spawned object itself.

The problem is that the Line is rendered at a different position than the position of the e.g. upperLine Gameobject. I think this is because ChoosenUp and ChoosenLow calculate different random numbers for each use. How can I reference to the value of ChoosenUp and ChoosenLow?

Here is what I got so far:

using UnityEngine;
using System.Collections;

public class SetRenderer : MonoBehaviour {

   private SpriteRenderer spriteRenderer;
   public GameObject upperLine;  // those are the Gameobjects that carry the LineRenderers
   public GameObject lowerLine;

   public float leftAlign; // Variable for the relative alignment
   public float rightAlign;

   void Start () {
     // Setting x-values for the Lines to be spawned
     spriteRenderer = gameObject.GetComponent<SpriteRenderer> ();
     float boundMin = spriteRenderer.bounds.min.x;  
     float boundMax = spriteRenderer.bounds.max.x; 
  
     // Setting random y-values for the Lines to be spawned
     float MaxY = spriteRenderer.sprite.bounds.max.y;
     float MinY = spriteRenderer.sprite.bounds.min.y;
     float ChoosenUp = Random.Range (MinY, MaxY);     // Y-value for the upperLine
     float ChoosenLow = Random.Range (MinY, ChoosenUp);  // Y-value for the lowerLine

  Instantiate(upperLine, new Vector3(0f, ChoosenUp, 0f), Quaternion.identity); // instantiating the Gameobjects
  Instantiate(lowerLine, new Vector3(0f, ChoosenLow, 0f), Quaternion.identity);

     LineRenderer Upper = upperLine.GetComponent<LineRenderer> ();   // Casting the actual lines
     Upper.SetPosition (0, new Vector3(boundMin, upperLine.transform.position.y, 0f));
     Upper.SetPosition (1, new Vector3(boundMax, upperLine.transform.position.y, 0f));
   
     LineRenderer Lower = lowerLine.GetComponent<LineRenderer> ();
     Lower.SetPosition (0, new Vector3(boundMin, lowerLine.transform.position.y, 0f));
     Lower.SetPosition (1, new Vector3(boundMax, lowerLine.transform.position.y, 0f));
   
     Debug.Log (ChoosenUp);
     Debug.Log (ChoosenLow);
}
}

Thanks in advance :slight_smile:

Try setting .useWorldSpace to true on both LineRenderers. Right it’s likely in local space, which will cause you to “double dip” its position.

I tried:

LineRenderer Upper = upperLine.GetComponent<LineRenderer> ();
        Upper.useWorldSpace = true;
        Upper.SetPosition (0, new Vector3(boundMin, ChoosenUp, 0f));
        Upper.SetPosition (1, new Vector3(boundMax, ChoosenUp, 0f));

But it wont work :confused:

The debug.log gives me the transform.position.y of the Renderer object, while the position of the actual lines is totally different. It doesnt even work for:

LineRenderer Upper = upperLine.GetComponent<LineRenderer> ();
        Upper.useWorldSpace = true;
        Upper.SetPosition (0, new Vector3(boundMin, upperLine.transform.position.y, 0f));
        Upper.SetPosition (1, new Vector3(boundMax, upperLine.transform.position.y, 0f));