Why doesnt 'Instantiate' not exist according to Unit

41 Views Asked by At

I'm encountering an issue in my Unity script where the Instantiate method is not being recognized. I'm trying to instantiate a projectile in a ranged attack state, but I'm getting the following error:

Assets\Scripts\RobotBehaviour\States\RobotAttackStateRanged.cs(48,33): error CS0103: The name 'Instantiate' does not exist in the current context

I've ensured that the UnityEngine namespace is imported at the top of my script, and the Unity version I'm using should support the Instantiate method. Despite these checks, the error persists.

Here's the relevant code snippet:

// Inside AttackPlayerRanged method GameObject projectile = Instantiate(enemyProjectile, RangedAttackSpawnPoint.position, RangedAttackSpawnPoint.rotation);

Here is the entire script this code is a part of, if it will be useful:

using UnityEngine;
using UnityEngine.AI;

public class RobotAttackStateRanged : RobotBaseState
{
    public NavMeshAgent agent;
    public bool readyToShoot;
    public Transform playerTransform;
    public Transform robottransform;
    public float throwCooldown = 5f;
    public GameObject enemyProjectile;
    public Transform RangedAttackSpawnPoint;
    public float projectileForce;

    public override void EnterState(RobotStateManager robot)
    {
        Assign(robot);
        Debug.Log("Attack State Mode: Ranged Activated!");
    }

    public override void UpdateState(RobotStateManager robot)
    {
        // Other update logic if needed
    }

    public override void FixedUpdateState(RobotStateManager robot)
    {
        AttackPlayerRanged(robot);
    }

    public override void OnCollisionEnter(RobotStateManager robot)
    {
        // Handle collision logic if needed
    }

    public override void Assign(RobotStateManager robot)
    {
        agent = robot.agent;
        playerTransform = robot.playerTransform;
        robottransform = robot.robottransform;
        enemyProjectile = robot.enemyProjectile;
        RangedAttackSpawnPoint = robot.RangedAttackSpawnPoint;
        projectileForce = robot.projectileForce;
    }

    private void AttackPlayerRanged(RobotStateManager robot)
    {
        readyToShoot = false;

        if (enemyProjectile != null && RangedAttackSpawnPoint != null)
        {
            // Getting direction from the enemy to the player
            Vector3 directionToPlayer = (playerTransform.position - robottransform.position).normalized;

            // LookAt makes the enemy face the player
            agent.transform.LookAt(playerTransform);

            // Instantiating projectile to be fired at the player
            GameObject projectile = Instantiate(enemyProjectile, RangedAttackSpawnPoint.position, RangedAttackSpawnPoint.rotation);

            // Acquiring rigidbody component so that the object can be fired using Unity physics engine
            Rigidbody projectileRb = projectile.GetComponent<Rigidbody>();

            if (projectileRb != null)
            {
                // DirectionToPlayer used to determine the force direction
                projectileRb.AddForce(directionToPlayer * projectileForce, ForceMode.Impulse);
            }
            else
            {
                Debug.LogError("Rigidbody component not found on the projectile.");
            }
        }
        else
        {
            Debug.LogError("Enemy projectile or spawn point not assigned.");
        }

        robot.Invoke(nameof(ResetEnemyShoot), throwCooldown);
    }

    private void ResetEnemyShoot()
    {
        readyToShoot = true;
    }
}

Any insights or suggestions on what might be causing this issue would be greatly appreciated.

I tried to instantiate an object so that I can then fire it at the enemy, but before I can even run the program I'm told that instantiate doesn't exist. For the record I'm using Unity 2022.3.1f1, which I believe is quite a recent version, so I'm not sure why else it isn't being detected.

0

There are 0 best solutions below