• 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 /
avatar image
0
Question by mushiehippo · Dec 04, 2012 at 12:47 PM · aijava

an ai script showing errors that make no sense java

I'm going from c# to java with scripts in a project I'm working on, but my code keeps coming up with errors that don't make sense to me. can someone help me fix the issues with my code please? here is the code

 var attackrange = 75;
 var target:Transform;
 var maxhealth = 25;
 var currenthealth = 25;
 var sightrange = 95;
 var movespeed = 25;
 var rotationspeed = 25;
 var attackrepeattime = 2;
 var giveUpThreshold = 95;
 var chaseThreshold = 85;
 var waypoint;
 var currenttarget:Transform;
 
 var mytransform:Transform;
 
 
 function awake (){
 
     myTransform = transform;
 }
 function start (){
 
     target = GameObject.FindWithTag("RWaypoint").transform;
 }
 function Update (){
 
  transform.LookAt(target);
  
  
     if(GameObject.FindWithTag("Bplayer").target){
         mytransform.position += mytransform.forward * movespeed * Time.deltaTime;
         }
     if(GameObject.FindWithTag("BMinion").target){
         mytransform.position += mytransform.forward * movespeed * Time.deltaTime;
         }
     if(GameObject.FindWithTag("BBuilding").target){
         mytransform.position += mytransform.forward * movespeed * Time.deltaTime;
         }
     if(GameObject.FindWithTag("RWaypoint").target){
         mytransform.position += mytransform.forward * movespeed * Time.deltaTime;
         }
     
         if(GameObject.currenttarget.distance > sightrange){
             currenttarget.changetarget;
             }
                 if((GameObject.currenttarget).currenthealth =< 0){
                     changetarget = true;
             }
             
             else{
             
                 if((GameObject.currenttarget).currenthealth > 0){
                     changetarget = false;
             }
         }
         if((mytransform.currenthealth) = 0){
             death = true;
         }
     
 }
 function attack (){
     if((target.distance) =< attackrange){
         target.currenthealth - 25;
     }
 }
 function chase (){
     if((target.distance) =< giveUpThreshhold){
         currenttarget.changetarget;
     }
 }
 function changetarget(){
 }
 function death(){
 }
 
Comment
Add comment · Show 3
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 mushiehippo · Dec 04, 2012 at 04:26 PM 0
Share

the errors im getting are things like found =, expecting ) found = expecting : unexpected symbol >. the program is meant to go to waypoints and if something comes close, to target it and attack it, but if its get to far away change target to closer enemy and if none then go to the waypoint. the c# script never changed target and could not access my players health properly so to the advice of my friends I changed it to java expecting more leway.

avatar image mushiehippo · Dec 04, 2012 at 04:45 PM 0
Share

im not wanting someone to program it for me, I need the learning. I just need to know what im doing wrong, a basic idea of how to fix it (with some code snippets maybe) and an idea of how to do things that I havent started in the code yet (like maybe dying [I have done destroy game object but it messis with my script for players] and possibly a way to prioritize buildings?)

avatar image MibZ · Dec 04, 2012 at 04:53 PM 0
Share

To my knowledge there isn't anything you can access with Javascript that you can't with C#, you must have had an error with your reference; while you're learning how to program you shouldn't switch languages because you haven't figured out the one you started with, especially if you're changing from C# to Javascript, because they're very very similar languages.

It's true that Javascript gives you more leeway, but in the long run that will prove to be a bad thing. Javascript allows you to declare a variable for an integer and doesn't care if later on you decide you want to store strings in it, but as cool as that might sound it is a bad practice. If you start using one variable for multiple types at some point you are going to get results that don't make sense because the variable contains something other than what you want it to.

That will be the biggest difference you notice other than slight changes in syntax, but they're very slight. If you have a basic understanding of C# you can easily figure out how to read and code Java and vice versa. I would recommend sticking with C# while you're learning, but it's your choice.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Dec 04, 2012 at 01:04 PM

You should show which errors you're having! Anyway, at first sight there are several weird things in this script:

1- GameObject.FindWithTag("...").target: FindWithTag returns a GameObject reference, and the GameObject doesn't have any target property, thus all lines like this will cause errors;
2- GameObject.currentTarget: currentTarget isn't a GameObject property either, thus all lines using it will produce errors.

I suggest you to edit your question and post the original C# script, so that we can have a better idea about what's going wrong.

Comment
Add comment · 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
0

Answer by MibZ · Dec 04, 2012 at 04:44 PM

The errors you mention in a comment on your original post are code errors, not runtime errors - it means you have at least one line of code typed incorrectly.

Reading over your code once, I found these.

 if((GameObject.currenttarget).currenthealth &#61;&lt; 0){
               changetarget = true;
          }

You need to change =< to <=. Remember, less than < or equal to =, not equal to or less than.

 if((mytransform.currenthealth) &#61; 0){
          death = true;
        }

You need to change '=' to '=='. One = is the assignment operator, used to assign the value on the right of the sign into the variable on the left side of the sign, while == is used to compare the values on either side.

 if((target.distance) &#61;&lt; attackrange){
        target.currenthealth - 25;
     }

Another '=<' to '<='

 if((target.distance) &#61;&lt; giveUpThreshhold){
        currenttarget.changetarget;
     }

And again. I would read up on your code syntax and order of operations, you have a lot of extra groups of parenthesis - that isn't a problem, but by adding extras you're just increasing the chances that you might leave one of them out and not be able to figure out where it is.

Comment
Add comment · Show 4 · 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 mushiehippo · Dec 04, 2012 at 05:13 PM 0
Share

indeed the changing of the symbols did fix my issues with those, but there now is the issue where it doesn't take away the players health >.> I have adjusted the script to read ' if((target.distance) <= attackrange){ eh = (playercurrenthealth)target.GetComponent("playercurrenthealth"); eh.addjustcurrenthealth(-10);'

but not it says it needs a ; at the end of the line ("playercurrenthealth") even though there already is one.

avatar image MibZ · Dec 04, 2012 at 05:29 PM 0
Share

Oh, that's an easy one too. Surprised I didn't notice it when I read it over the first time.

In order to update what is in a variable you have to assign to it, you can't just use a $$anonymous$$us sign. In order to do that you can either write it out the literal way 'target.currenthealth = target.currenthealth - 25;' or you can write it out the shorthand way 'target.currenthealth -= 25;'.

It works the same way for all standard math operations, -= += *= /= all will turn something like 'x = x + y;' into 'x += y'. Just make sure you remember what the operators do, because there are a lot more in program$$anonymous$$g than there are in handwritten math taught in school.

avatar image mushiehippo · Dec 04, 2012 at 05:46 PM 0
Share

well thank you for your help, but so many more questions to answer before this script is ready for use (like it dosnt actually target anything unless told to, and then it never changes target >.>)

avatar image MibZ · Dec 04, 2012 at 06:11 PM 0
Share

Do you have code inside of your changetarget function? In the snippet you posted it is an empty function, if you haven't added any code to that it is doing exactly what you told it to do: nothing.

I'm not sure how much of this works without running it myself, but I would change 'if(GameObject.currenttarget.distance > sightrange)' to 'if (Vector3.Distance(gameObject.transform.position, currenttarget.position) > sightrange)'. I don't think what you have written is actually calculating distance.

You should also start using 'Debug.Log()', it lets you write data to the Console window of the Editor while the game is running so you can tell what values variables hold at a particular point during runtime. Ex "Debug.Log(Vector3.Distance(gameObject.transform.position, currenttarget.position));" will print the distance between the ai and its target.

avatar image
0

Answer by mushiehippo · Dec 04, 2012 at 10:33 PM

my c# script is broken down into 3 seperet programs

' using UnityEngine;

 using System.Collections;
 
 
 
 public class enemyair : MonoBehaviour {
 
     private Transform Target;
 
     public int movespeed;
 
     public int rotationspeed;
 
     public float maxdis;
 
     
 
     
 
     private Transform mytransform;
 
     
 
     void Awake() {
 
         mytransform = transform;
 
     }
 
 
 
     // Use this for initialization
 
     void Start () {
 
         GameObject go = GameObject.FindGameObjectWithTag("Bplayer");
 
         
 
         Target = go.transform;
 
         
 
         maxdis = Vector3.Distance(Target.transform.position, transform.position);
 
     
 
     }
 
     
 
     // Update is called once per frame
 
     void Update () {
 
         Debug.DrawLine(Target.position, mytransform.position, Color.red);
 
                       
 
           //look at target
 
         mytransform.rotation = Quaternion.Slerp(mytransform.rotation, Quaternion.LookRotation(Target.position - mytransform.position), rotationspeed * Time.deltaTime);
 
             
 
         if(maxdis < 50){
 
             movespeed = 0;
 
             if(maxdis>50){
 
                  movespeed=25;
 
         
 
         }
 
     }
 
         
 
         if(Target=null){
 
             Target=Target.transform;}
 
         
 
         if(Vector3.Distance(Target.position, mytransform.position) > maxdis) {
 
         //move towars target
 
         mytransform.position += mytransform.forward * movespeed * Time.smoothDeltaTime;
 
         
 
     
 
 
 
     }
 
 }
 
 }'

for movement

' using UnityEngine;

 using System.Collections;
 
 
 
 public class enemyattack : MonoBehaviour {
 
     public GameObject target;
 
     public float attacktimer;
 
     public float cooldown;
 
 
 
     // Use this for initialization
 
     void Start () {
 
         attacktimer = 0;
 
         cooldown = 2.0f;
 
     
 
     }
 
     
 
     // Update is called once per frame
 
     void Update () {
 
         if(attacktimer > 0)
 
             attacktimer -= Time.deltaTime;
 
         
 
         if(attacktimer < 0)
 
             attacktimer = 0;
 
         
 
         
 
         if(attacktimer == 0) {
 
         attack();
 
         attacktimer = cooldown;
 
         }
 
     if(target=null){
 
             target=GameObject.FindGameObjectWithTag("BMinion");
 
     }
 
     }
 
     private void attack() {
 
         float distance = Vector3.Distance(target.transform.position, transform.position);
 
         
 
         Vector3 dir =(target.transform.position - transform.position).normalized;
 
         
 
         float direction = Vector3.Dot(dir, transform.forward);
 
         
 
         Debug.Log(direction);
 
         
 
         Debug.Log(distance);
 
         
 
         if(distance < 50) {
 
             if(direction > 0) {
 
                playerhealth eh = (playerhealth)target.GetComponent("playerhealth"); 
 
                     eh.addjustcurrenthealth(-10);
 
                 
 
             }
 
         }
 
     }
 
 }'

for attack and

 using UnityEngine;
 
 using System.Collections;
 
 
 
 public class enemyhealth : MonoBehaviour {
 
     public int maxhealth = 100;
 
     public int curhealth = 100;
 
     
 
     public float healthbarlength;
 
     
 
     // Use this for initialization
 
     void Start () {
 
            healthbarlength = Screen.width /6;
 
     }
 
     
 
     // Update is called once per frame
 
     void Update () {
 
         addjustcurrenthealth(0);
 
     
 
     }
 
     
 
     void OnGUI() {
 
         GUI.Box(new Rect(20, 30, healthbarlength, 20), curhealth + "/" + maxhealth);
 
     }
 
     
 
     public void addjustcurrenthealth(int adj) {
 
         curhealth += adj;
 
         
 
         if(curhealth < 0)
 
             curhealth = 0;
 
         
 
         if(curhealth > maxhealth)
 
             curhealth = maxhealth;
 
         
 
         if(maxhealth < 1)
 
             maxhealth = 1;
 
         
 
         
 
     if(curhealth <= 0)
 
 {
 
             
 
    Destroy(gameObject);
 
     
 
                 
 
     
 
         
 
         healthbarlength = (Screen.width / 2) * (curhealth / (float)maxhealth);
 
 
 
 }
 
     }
 
 

for health

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use of an AI script, and having it NOT go through walls using a character controller 0 Answers

How to make a simple AI escape script? 2 Answers

Targeting other objects of the same type but not self 1 Answer

How to create a simple Enemy AI for a 2D game? Mine isn't working. . . 1 Answer

Having trouble with enemy AI and Collider 0 Answers


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