Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Shadow_Stalker · Oct 29, 2016 at 05:23 AM · scripting problemerrorcompiler-error

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shadow_Stalker · Oct 29, 2016 at 10:24 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

All scripts in unity give the error "the associated script can not be loaded please fix any compile errors and assign a vaild script" 4 Answers

Why do I keep getting this error? 1 Answer

warning CS0414: The private field `GameManagerSingleton.text' is assigned but its value is never used 0 Answers

Camera Switch error 0 Answers

how to fix cs1014 error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges