I’m trying to make a projectile that follows that player but I’m having trouble defining the location of the player. The error I’m getting is:
Assets\Scripts\missile.cs(12,25): error CS0120: An object reference is required for the non-static field, method, or property ‘GameObject.GetComponent()’
How can I fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class missile : MonoBehaviour
{
public float missileSpeed = 10f;
public GameObject explosion;
public bool lockedOn = false;
public Transform player;
GameObject target = GameObject.GetComponent<scoregame>(); //(scoregame is the player's script)
// Start is called before the first frame update
void Start()
{
Invoke("Die", 10f);
Invoke("LockOn", 2.5f);
Invoke("StopLockingOn", 7.5f);
}
// Update is called once per frame
void Update()
{
if (lockedOn == true)
{
transform.LookAt(target.transform);
}
}
void LockOn()
{
lockedOn = true;
missileSpeed = 20;
}
void StopLockingOn()
{
lockedOn = false;
}
void Die()
{
GameObject attack = Instantiate(explosion, transform.position, transform.rotation);
gameObject.SetActive(false);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject)
{
Invoke("Die", 0);
}
}
}