gameObject not dynamically scaling via the update method

Hello there!

I am currently working in 2D and trying to make a ‘lazer-beam’ sort of thing appear between the player and where a raycast hits. So far, I have got the beam to instantiate well enough at the right scale and position.

However, I want the beam to scale dynamically between the player and the end of the raycast, so that if the player moves further away from where it is hitting, the beam stretches so that it is still connected to the player.

I attempted to do this by putting the scaling code in the update function and assuming that it would update the scale every frame. Instead, the beam stays the length it was instantiated at and is seemingly unaffected by the code in the update function.

Here is the code I am using:

public float fireRate = 0;
	public float damage = 0;
	public LayerMask whatToHit;
	public GameObject lazer;

	float timeToFire = 0;
	Transform firePoint;
	float distance = 0;
	Vector2 lazerInstantiate;
	Vector2 lazerPosition;
	Vector2 firePointPosition;
	RaycastHit2D hit;

	// Use this for initialization
	void Start () {
		firePoint = transform.FindChild ("FireLazerPoint");
		if (firePoint == null)
		{
			Debug.LogError ("NO FIREPOINT LULZ!??!!??!!");
		}
	}
	
	// Update is called once per frame
	void Update () {
				if (Input.GetButtonDown ("Fire1") && Time.time > timeToFire) {
						timeToFire = Time.time + 1 / fireRate;
						Shoot ();
				}
		}

	void Shoot() 
	{

		lazerPosition = new Vector2(firePoint.position.x + 100, firePoint.position.y);
		firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
		hit = Physics2D.Raycast (firePointPosition, lazerPosition - firePointPosition, 100, whatToHit);
		Debug.DrawLine (firePointPosition, (lazerPosition - firePointPosition)*100, Color.cyan);
		Instantiate (lazer, Vector2.Lerp (firePointPosition, hit.point, 0.5f), Quaternion.identity);
		distance = Vector2.Distance (firePointPosition, hit.point);
		lazer.transform.localScale = new Vector2 (distance, 1);


		Debug.Log (distance);

		if (hit.collider != null) 
		{
			Debug.DrawLine(firePointPosition, hit.point, Color.red);

		}

	}

From my meager knowledge of unity and c#, I have no clue as to why this isn’t working! Any advice on the probably obvious blunder I’ve made would be much appreciated :slight_smile:

Cheers!

P.S. the debug drawlines are just there for testing purposes, I know they don’t show up in game.

Your starting point is never updated, since the Start function is only called once (it act like a constructor for your object)

Thus, your update function needs to read :

     void Update () {
             if (Input.GetButtonDown ("Fire1") && Time.time > timeToFire) {
                     timeToFire = Time.time + 1 / fireRate;

                     //Just add this line so you always get an up-to-date firePoint :
                     firePoint = transform.FindChild ("FireLazerPoint");

                     Shoot ();
             }
     }