# Why can't I reference "targetVelocity" variable to this script?

Recently, I implemented a shooting system for my 2D game but this system has a problem. The problem is that when the player is facing left, the bullets are still being shoot to the right. For fixing this, I need to access a Vector2 named targetVelocity to check whether the player is facing right or not. targetVelocity is defined in another script called PhysicsObject that is specifically for the player physics and the player script inherits from it (the variable was protected but I changed it to public). I just don't know how to reference this in my Bullet script and I don't want to add the player script to the bullet. Also, I've read the past questions and answers that were similar to this but I haven't found the answer yet because this is a different case. Thanks in advance.

This is my Bullet script and I want to somehow reference targetVelocity.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
[SerializeField] float speed;

public GameObject destroyEffect;

private void Start()
{
}

private void Update()
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}

void DestroyBullet()
{
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}

}


Here is the weapon script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Weapon : MonoBehaviour
{
public GameObject bullet;
public Transform firePoint;

private float timeBtwShots;
public float startTimeBtwShots;

private void Update()
{
if (timeBtwShots <= 0)
{
if (Input.GetButton("Fire1"))
{
Instantiate(bullet, firePoint.position, transform.rotation);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
}


This is the player script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewPlayer : PhysicsObject
{
[SerializeField] private float jumpPower = 10;
[SerializeField] private float maxSpeed = 1;

//Singleton instantation
private static NewPlayer instance;
public static NewPlayer Instance
{
get
{
if (instance == null) instance = GameObject.FindObjectOfType<NewPlayer>();
return instance;
}
}

// Update is called once per frame
void Update()
{
targetVelocity = new Vector2(Input.GetAxis("Horizontal") * maxSpeed, 0);
//If the player presses "Jump" and we're grounded, set the velocity to a jump power value
if (Input.GetButtonDown("Jump") && grounded)
{
velocity.y = jumpPower;
}
//Flip the player's localScale.x if the move speed is greater than .01 or less than -.01
if (targetVelocity.x < -.01)
{
transform.localScale = new Vector2(-1, 1);
}
else if (targetVelocity.x > .01)
{
transform.localScale = new Vector2(1, 1);
}
}
}

• That's exactly my problem. NewPlayer.Instance.targetVelocity.x is not working in the bullet script. I get the name does not exist in the current context error.
– Ali
Dec 29, 2020 at 18:44
• Okay, I'll try that. But I have a question. You created a gun-like object with fire point and the bullets' direction changes based on the player facing direction, is that right? I'm not sure how it worked for you.
– Ali
Dec 29, 2020 at 19:06
• My question is how to reference the target velocity in the bullet script and I need to have the weapon and bullet. Because the whole point is to reference target velocity in the bullet script in order to adapt the player facing direction to the bullet. Also, you need to use the physics object code because the variable was defined there for the first time. Could you show the line that you wrote in the script for referencing the target velocity?
– Ali
Dec 29, 2020 at 19:39
• Dec 29, 2020 at 19:49
• In future, please do not delete and re-post a question that has been put on hold. Instead, edit the existing question to resolve the issue so it can be re-opened. Deleting and re-posting can look like an attempt to circumvent moderation from the perspective of StackExchange's automated scripts, and can result in restrictions to your account. Dec 30, 2020 at 4:41

The easy fix for your bullet is to grab once the direction on the start of creating your bullet and just let it fly that way. There could be more elegant solutions and the reference to the targetVelocity seems weird but this is what you asked for.

private Vector2 fireDirection;
private void Start()
{
fireDirection = new Vector2(Math.Sign(NewPlayer.Instance.targetVelocity.x), 0);
Invoke("DestroyBullet", 2);
}

private void Update()
{
transform.Translate(fireDirection * speed * Time.deltaTime, Space.World);
}