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