Unity C# question

I’m having some issues trying to work on a script for a FPS. I keep getting errors that something is wrong in my script and I’m not sure how to fix it. Mind you, I’m fairly new with C#.

Some of the errors I am getting are these:

If anyone can help me out with this, it would be greatly appreciated. Thank you.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterSystem))]

public class AICharacterController : MonoBehaviour
{

    public GameObject ObjectTarget;
    public string TargetTag = "Player";
    private CharacterSystem character;
    private int aiTime = 0;
    private float scanFrequency = 1.0f;
    private int aiState = 0;

    void Start()
    {
        character = gameObject.GetComponent<CharacterSystem>();
        InvokeRepeating("ScanForTarget", 0, scanFrequency);
    }

    void ScanForTarget()
    {
        ObjectTarget = GetNearestTaggedObject();
    }

    public GameObject GetNearestTaggedObject()
    {
        var nearestDistanceSqr = Mathf.Infinity;
        GameObject nearestObj = null;

        foreach (var obj in GameObject.FindGameObjectsWithTag(TargetTag))
        {

            var objectPos = obj.transform.position;
            var distanceSqr = (objectPos - transform.position).sqrMagnitude;

            if (distanceSqr < nearestDistanceSqr)
            {
                nearestObj = obj;
                nearestDistanceSqr = distanceSqr;
            }
        }
        return nearestObj;
    }

    public float DistanceSquaredTo(GameObject source, GameObject target)
    {
        return Vector3.SqrMagnitude(source.transform.position - target.transform.position);
    }

    void Update()
    {
        //if (GameObject.Find("CharacterDakota").GetComponent<CharacterStatus>().HP > 0) {

        if (GameObject.Find("GameManager").GetComponent<GameManager>().Playing)
        {

            var direction = Vector3.zero;
            if (aiTime <= 0)
            {
                aiState = Random.Range(0, 4);
                aiTime = Random.Range(10, 100);
            }
            else {
                aiTime--;
            }
            if (ObjectTarget)
            {
                //ObjectTarget = GameObject.FindGameObjectWithTag(TargetTag);   
                float distance = Vector3.Distance(ObjectTarget.transform.position, this.gameObject.transform.position);

                if (distance <= 2)
                {
                    transform.LookAt(ObjectTarget.transform.position);
                    if (aiTime <= 0)
                    {
                        if (aiState == 1)
                        {
                            character.Attack();
                        }
                    }
                }
                else {
                    if (aiState == 1)
                    {
                        transform.LookAt(ObjectTarget.transform.position);
                        direction = this.transform.forward;
                        direction.Normalize();
                        character.Move(direction);
                    }
                }

            }
            else {
                ScanForTarget();
            }
        }
    }
}

You may be missing the CharacterSystem in your project folder or the Visual Studio’s solution.

First one you can fix by importing the package. Second one you can fix through Visual Studio by closing and reopening the solution.

Not sure if something else is wrong here.

Solution 1

I didn’t use RequireComponent before but, at least I guess so, that function tries to get the Component of type XY and uses it in your script. From what I have found in the scripting reference of RequireComponent, I would say that you simply didn’t add a Component of type CharacterSystem to your Gameobject which the script is attached to.


Solution 2

You didn’t add Character System correctly to your Project.
From what I have seen in your script you never added a reference for CharacterSystem in your script.
To do so, just add " namespace CharacterSystem{ //Script…} " at the start of your scripts.


Solution 3

CharacterSystem is broken.
Typo somewhere.


Solution 4

I guess you probably want to get the CharacterSystem Component attached to your gameobject.
I wouldn’t use RequireComponent for that.
If that’s what you wanted to achieve, just define a variable of type CharacterSystem and assign the value at runtime.

public CharacterSystem char;

void start()
{
char=getComponent<CharacterSystem>();

}

This is what you did as well.
There’s probably no need to Require any Component since you’re already trying to get the reference with getComponent.