Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# Coding Help Please
#1
Ok so I am working on a Boxing minigame for a game Im working on for an after school program and Im stuck on how to fix the errors. The errors are in red and what is wrong with it is wrong with it is summed up in italics and blue (according to microsoft visual studio). Please help me.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public enum EnemyState {
    CHASE,
    ATTACK
}

public class EnemyController : MonoBehaviour {

    private NavMeshAgent navAgent;
    private Transform playerTarget;
    private EnemyState enemy_State;

    public float move_Speed = 3.5f;
    public float attack_Distance = 1f;
    public float chase_Player_After_Attack_Distance = 1f;

    private float wait_Before_Attack_Time = 3f;
    private float attack_Timer;

    void Awake() {
        navAgent = GetComponent<NavMeshAgent>();

        playerTarget = GameObject.FindGameObjectsWithTag("Player").transformCode: CS1061 transform is not defined nor accessible extension method;

    }

    void Start() {
        enemy_State = EnemyState.CHASE;

        attack_Timer = wait_Before_Attack_Time;
    }

    void Update() {
        if(enemy_State == EnemyState.CHASE) {
            ChasePlayer();
        }
        if(enemy_State == EnemyState.ATTACK) {
            AttackPlayer();
        }
    }

    void ChasePlayer() {
        navAgent.SetDestination(playerTarget.position);
        navAgent.speed = move_Speed;

        if(Vector3.Distance(transform.position, playerTargetCode: CS1503 Cannot convert UnityEngine.transform to UnityEngine.Vector3) <= attack_Distance) {
            enemy_State = EnemyState.ATTACK;
        }
    }

    void AttackPlayer() { 
    
    }

}
Reply
#2
Well, I am not a computer science major. I do have a bit of experience with Java/JS. First of all, try putting parentheses after transform.
If that doesn't work that means you're either wrong with 'transform' or something prior to the . in line 27, or the line with transform in it.
Because there's an issue prior in the code with what playerTarget equal's there will obviously be trouble in line 50 once you reintroduce playerTarget.


I've probably only told you what you already know, sorry about that! Good luck with your game! (It's also really hard because I have little to no experience with C# and it's kinda confusing when you don't know the context.)
I hope what I've told you helps you, even if just a little.
Reply
#3
I added .posistion after playerTarget to fix the coding error in line 50. I fixed the error in line 27 by taking out the s in FindGameObjectsWithTag
Reply
#4
You tried to access transform from an object at playerTarget = GameObject.FindGameObjectsWithTag("Player").transformCode: CS1061 transform is not defined nor accessible extension method;

You're just forgetting/missing more parameters that relates to transform.position at playerTargetCode: CS1503 Cannot convert UnityEngine.transform to UnityEngine.Vector3
Reply
#5
I’m the future add some comments comments above a function or class. It will make it a lot easier to debug your code, and for other people in the future to help.

/**
* Name and description
* @param namOfParameter.
* @return nameOfWhatsBeingReturned
*/

Don’t use the underscore text thing your doing when declaring constants it read only variables. Should be PascalCasing in your code. I didn’t see this here but don’t use underscores in identifiers, unless it’s private then you can have a leading underscore.

Also good use of an enumeration
Reply
#6
(12-02-2019, 09:47 PM)Tedgp908 > Wrote: I’m the future add some comments comments above a function or class. It will make it a lot easier to debug your code, and for other people in the future to help.

 /**
  * Name and description
  * @param namOfParameter.  
  * @return nameOfWhatsBeingReturned
  */

Don’t use the underscore text thing your doing when declaring constants it read only variables. Should be PascalCasing in your code. I didn’t see this here but don’t use underscores in identifiers, unless it’s private then you can have a leading underscore.

Also good use of an enumeration

Thank you. I will definitely use comments next time i code. So for Pascal Casing. I dont know much about that so would it be like: private EnemyState EnemyState; instead of private EnemyState enemy_State;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)

About Us
    This is Dinkleberg's GMod, a gaming community based in Garry's Mod. We have a Trouble in Terrorist Town, and Prop Hunt Server. Come check them out sometime.