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 SSJ-Panda · Jan 06, 2016 at 09:24 PM · c#java to c#error-cs1525

Issues with c# script, minor error that I am unsure how to fix

Hi I am having issues with the following piece of code:

{

 public float minTarX = 1;
 public float maxTarX = 10;
 public float minTarZ = 1;
 public float maxTarZ = 10;
 public float tarX;
 public float tarZ;
 
 public float dampX;
 public float dampZ;
 int timeSwitch = 100;
 public GameObject target;
 
 void  Start ()
 {
     CreateTarPoint();
 }
 
 void  Update ()
 {
     if(target != null)
     {
         transform.GetComponent<"NavMeshAgent">().destination = target.position;
     }
     else
     {
         if(timeSwitch <= 0)
         {
             timeSwitch = 100;
             CreateTarPoint();
         }
         else
         {
             transform.GetComponent<"NavMeshAgent">().destination = new Vector3(tarX, 0, tarZ);
             timeSwitch -=1 * Time.deltaTime;
         }
     }
 }
 
 void  CreateTarPoint ()
 {
     dampX = Random.Range(1.0f, 3.0f);
     dampZ = Random.Range(1.0f, 3.0f);
     
     tarX = Random.Range(minTarX, maxTarX) - dampX;
     tarZ = Random.Range(minTarZ, maxTarZ) - dampZ;
 }
 
 void  OnTriggerEnter ( Collider col  )
 {
     if(col.gameObject.tag == "Player")
     {
         target = col.transform;
     }
 }
 
 void  OnTriggerExit ( Collider col  )
 {
     if(col.gameObject.tag == "Player")
     {
         target = null;
     }
 }
 

}

Specifically I am having issues here:

void Update () { if(target != null) { transform.GetComponent<"NavMeshAgent">().destination = target.position; } }

Where is says: <"NavMeshAgent">().destination = target.position; I have a red error with the () parenthesis, I remove them and then I have another error with the full stop, I remove that and then I receive another error with the = symbol. I used a conversion system to bring this code to c# from Javascript. Would someone please be able to assist me? Thanks in advance.

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
Best Answer

Answer by Kielgasten · Jan 06, 2016 at 09:49 PM

Hi,

There might be other things, but start with removing the "" around <"NavMeshAgent">

This is to say the lines should look like this:

  transform.GetComponent<NavMeshAgent>().destination = new Vector3(tarX, 0, tarZ);

As an aside, consider caching your components in the Start(), that way you won´t spend resources finding the NavMeshAgent component every frame.

Comment
Add comment · Show 5 · 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 SSJ-Panda · Jan 06, 2016 at 10:00 PM 0
Share

Hi,

Thank you for your help, it fixed the issue I was having but the issue I am now having is this:

Error CS0266: Cannot implicitly convert type float' to int'. An explicit conversion exists (are you missing a cast?)

This error is in relation to this line:

timeSwitch -=1 * Time.deltaTime;

You wouldn't happen to have any advice on this would you?

avatar image Kielgasten SSJ-Panda · Jan 06, 2016 at 10:14 PM 0
Share

in the timeswitch declaration (at line 10) change int to float. C# enforces type safety. This means that once a variable has been declared (here an int) only int values can be assigned to it. On that particular line you are attempting to assign a float value to it.

Javascript does not do this, which is why it probably worked before.

avatar image SSJ-Panda Kielgasten · Jan 06, 2016 at 10:39 PM 0
Share

Thank you very much for your help. I think I have it sorted now, although the job of this script is to make an enemy move randomly, and the enemy isn't moving with the script attached :P but thank you anyway. If you were willing to help more this issue seems to be stem$$anonymous$$g from this:

public GameObject target;

In the tutorial I used (which was made in Java) the gentleman wrote var target : Transform on that line, which I figured changing it to be a public Transform might alleviate that issue but it seems to not be perfor$$anonymous$$g the originally intended function. The cube I have attached this script to won't move, the numbers are generated but the cube remains still, I have also baked a Navmesh onto a plane.

Show more comments

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

56 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to create a matrix in C#? 1 Answer

Convert Java to C# 1 Answer

JavaScript to C# 1 Answer

How to change from C# to JAVA? 4 Answers

Error CS1525: Unexpected Symbol ')' on lines 13 and 14 2 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