I am trying to make a point-and-shoot function for my game and I am receiving this error:
error CS1061: ‘GameObject’ does not contain a definition for ‘position’ and no accessible extension method ‘position’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?)
Any suggestions would be very appreciated.
Here is the code that I have come up with so far:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnProjectile2D_v2
: MonoBehaviour
{
public GameObject spawnPoint;
public GameObject projectTilePrefab;
public float projectTileSpeed = 1;
DateTime lastSpawnTime = DateTime.Now;
public int millisecondsBetweenProjectiles = 1;
public bool AutoFire = false;
public float secondsBeforeProjectileDies = 10;
public float secondsBeforeShotEffectsDestroy = 1;
public float Shots = 1;
public float reloadTime = 1000;
private Camera _cam;
[SerializeField] private float _rotationSpeed = 1;
public GameObject shotEffectsPrefab;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
var mousePosition = _cam.ScreenToWorldPoint(Input.mousePosition);
mousePosition.z = 0;
transform.up = Vector3.MoveTowards(transform.up, mousePosition, _rotationSpeed * Time.deltaTime);
if ((AutoFire || Input.GetMouseButtonDown(0)) && (DateTime.Now-lastSpawnTime).TotalMilliseconds > millisecondsBetweenProjectiles && Shots >= 1)
{
Instantiate(projectTilePrefab, spawnPoint.Transform.position, Quaternion.identity).Init(transform.up);
Shots = Shots - 1;
//do we have a "fire" image to play?
if (shotEffectsPrefab != null)
{
GameObject image = Instantiate(shotEffectsPrefab, spawnPoint.transform);
Destroy(image, secondsBeforeShotEffectsDestroy);
}
}
if(Shots < 1 && (DateTime.Now-lastSpawnTime).TotalMilliseconds > reloadTime)
{
Shots = Shots + 1;
}
}
}