Lots of compiler errors I don't know how to fix
I have 2 scripts that have errors I have never encountered before, the first script makes an animated character wander around an object, the errors are occurring in line 7-14
using UnityEngine;
using System.Collections;
public class RandomWander : MonoBehaviour {
public GameObject init;
private Transform initpos = init.GetComponent<Transform>();
private float time = 10;
private boolean update = true;
public float radius;
float lowX = initpos.x - radius;
float highX = initpos.x + radius;
float lowZ = initpos.z - radius;
float highZ = initpos.z + radius;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent <Animator>();
}
// Update is called once per frame
void Update () {
if (update) {
Vector3 pos = getRandomPos();
}
gameObject.LookAt(pos);
if (gameObject.transform.position == pos){
time -= Time.deltaTime;
anim.SetBool("Walking", false);
if (time <= 0.0f){
update = true;
time = 10.0f;
}
} else {
transform.position += transform.forward * 0.2f;
anim.SetBool("Walking", true);
}
}
Vector3 getRandomPos(){
Vector3 pos;
pos.x = Random.Range(lowX, highX);
pos.y = initpos.y;
pos.z = Random.Range(lowZ, highZ);
return pos;
}
}
Second script allows the FPS controller to attack these NPC's but there are errors with line 22 and 29 using UnityEngine; using System.Collections;
public class SwordAttack : MonoBehaviour {
public int frame = 0;
public GameObject sword;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//replace with left key check
if (Input.GetMouseButtonDown(0) && frame <= 10){
sword.transform.position += sword.transform.forward*0.1f;
++frame;
} else if (frame >= 11){
frame = 0;
sword.transform.position += sword.transform.back*0.1f*frame;
}
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Enemy"){
other.gameObject.GetComponent<Animator>().SetBool("Dead", true);
}
}
}
If anybody can resolve these issues it would be greatly appreciated.
Answer by Landern · Oct 29, 2016 at 05:34 AM
you can't assign fields in a class with fields, you should move the assignment into the Awake method or Start depending on your needs, the below listed should be declared then assigned/set as i mentioned.
// change these declarations
private Transform initpos;
float lowX;
float highX;
float lowZ;
float highZ;
//initialize then in Start or Awake methods
void Awake ()
{
initpos = init.GetComponent<Transform>();
lowX = initpos.x - radius;
highX = initpos.x + radius;
lowZ = initpos.z - radius;
highZ = initpos.z + radius;
}
You other issue is trying to use .back and .forwars on a Transform type and not a Vector3 type... like the position of the Transform type. fixed below.
sword.transform.position += sword.transform.position.forward*0.1f;
sword.transform.position += sword.transform.position.back*0.1f*frame;
The first script still seems to have issues with line 7, it says
Assets/RandomWander.cs(7,37): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `RandomWander.init'
Here is the code with the edits
using UnityEngine;
using System.Collections;
public class RandomWander : $$anonymous$$onoBehaviour {
public GameObject init;
private Transform initpos = init.GetComponent<Transform>();
private float time = 10;
private bool update = true;
public float radius;
float lowX;
float highX;
float lowZ;
float highZ;
// Use this for initialization
void Awake () {
anim = gameObject.GetComponent <Animator>();
initpos = init.GetComponent<Transform>();
lowX = initpos.x - radius;
highX = initpos.x + radius;
lowZ = initpos.z - radius;
highZ = initpos.z + radius;
}
// Update is called once per frame
void Update () {
if (update) {
Vector3 pos = getRandomPos();
}
gameObject.LookAt(pos);
if (gameObject.transform.position == pos){
time -= Time.deltaTime;
anim.SetBool("Walking", false);
if (time <= 0.0f){
update = true;
time = 10.0f;
}
} else {
transform.position += transform.forward * 0.2f;
anim.SetBool("Walking", true);
}
}
Vector3 getRandomPos(){
Vector3 pos;
pos.x = Random.Range(lowX, highX);
pos.y = initpos.y;
pos.z = Random.Range(lowZ, highZ);
return pos;
}
}
Your answer
Follow this Question
Related Questions
Why do I keep getting this error? 1 Answer
Camera Switch error 0 Answers
how to fix cs1014 error 1 Answer