In a script attached to my main camera I’ve got a ray casting forward to any object I put under a crosshair. If the object that’s being hit by the ray is a turret and the Q key is pressed then I want the turret to be upgraded. To do this I’m trying to run a method in the turret script from the camera script that will upgrade the turret. My problem here is that I’m getting an error when I call the method in the turret script:
An object reference is required for the non-static field, method, or property ‘Turret.UpgradeTurret()’
Having read through some information, I’ve found that making the upgrade turret function static will fix the problem, but it will also mean that all the turrets in the scene will be upgraded at the same time. Sorry if the question is a little simple, I’m just starting up with learning c#.
This is from the script calling the UpgradeTurret method.
if (targetTag == "Turret")
{
ChangeTurretMaterial(selectedGameObject, raySelected);
Turret turret = selectedGameObject.GetComponent<Turret>();
Turret.UpgradeTurret();
}
This is the methos being called.
public void UpgradeTurret()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Debug.Log("Upgrading Turret to level 2! You're bossing it!");
}
}
I’ve used this successfully elsewhere in the script and can’t see why this isn’t working. Thanks in advance, I’ll add the full scripts below should that be of any help.
Camera script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
private Transform _target;
public GameObject cam;
public Material raySelected;
public Material defaultMaterial;
public float rayDistance;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (_target != null && _target.tag == "Tower Base")
{
var targetRenderer = _target.GetComponent<Renderer>();
targetRenderer.material = defaultMaterial;
_target = null;
}
if (_target != null && _target.tag == "Turret")
{
ChangeTurretMaterial(_target.gameObject, defaultMaterial);
_target = null;
}
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, rayDistance))
{
var target = hit.transform;
string targetTag = target.tag;
GameObject selectedGameObject = target.gameObject;
if (targetTag == "Tower Base")
{
Renderer paintable = selectedGameObject.GetComponent<Renderer>();
Node node = target.GetComponent<Node>();
paintable.material = raySelected;
node.SpawnTurret(target.gameObject);
}
if (targetTag == "Turret")
{
ChangeTurretMaterial(selectedGameObject, raySelected);
Turret turret = selectedGameObject.GetComponent<Turret>();
Turret.UpgradeTurret();
}
_target = target;
}
}
public void ChangeTurretMaterial(GameObject _target, Material newMat)
{
GameObject selectedTurret = _target;
Renderer[] children;
children = _target.GetComponentsInChildren<Renderer>();
foreach (Renderer rend in children)
{
var mats = new Material[rend.materials.Length];
for (var j = 0; j < rend.materials.Length; j++)
{
mats[j] = newMat;
}
rend.materials = mats;
}
}
}
Turret script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Turret : MonoBehaviour
{
[Header("Turret movement")]
public Transform target;
public float range = 11.5f;
public string enemyTag = "Enemy";
public bool targeting = false;
public Transform GunPivot;
public float rotationSpeed = 10f;
[Header("Turret firing")]
public float fireRate = 1f;
public float fireCountdown = 0f;
public GameObject bullet;
public Transform firingPoint;
public int damage = 1;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("UpdateTarget", 0f, 0.5f);
}
void UpdateTarget()
{
float shortestDistance = Mathf.Infinity;empty.
GameObject nearestEnemy = null;
GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
foreach (GameObject enemy in enemies)
{
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToEnemy < shortestDistance)
{
shortestDistance = distanceToEnemy;
nearestEnemy = enemy;
}
}
if (nearestEnemy != null && shortestDistance <= range)
{
target = nearestEnemy.transform;
targeting = true;
}
else
{
target = null;
}
}
void Update()
{
if (target == null)
{
targeting = false;
return;
}
Vector3 dir = target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);to euler angles,...
Vector3 rotation = Quaternion.Lerp (GunPivot.rotation, lookRotation, Time.deltaTime * rotationSpeed).eulerAngles;
GunPivot.rotation = Quaternion.Euler(rotation.x, rotation.y, 0f);
fireCountdown -= Time.deltaTime;
if (fireCountdown <= 0)
{
Shoot();
fireCountdown = 1 / fireRate;
}
}
void Shoot()
{
GameObject bulletGO = (GameObject) Instantiate(bullet, firingPoint.position, firingPoint.rotation);
Bullet boomBall = bulletGO.GetComponent<Bullet>();
if (boomBall != null)
{
boomBall.seek(target);
}
}
public void UpgradeTurret()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Debug.Log("Upgrading Turret to level 2! You're bossing it!");
}
}
at which the turret can shoot.
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, range);
}
}