Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Nzingha · Oct 25, 2014 at 12:43 PM · variablereferencesyntax-error

Converting JS to C#

Hello Everyone, Unity and Scripting noob here. I am trying to convert my playercontrol.js file to a C# file for my game where the player is an sphere that traveling through a 3D city maze. I believe I got most of it done but there is an error with line 55 under void FixedUpdate ():

[private movement Vector3 = new Vector3 (moveHorizontal,0.0f,moveVertical); ]

The error is [Assets/Script/PlayerControl.cs(55,23): error CS1525: Unexpected symbol `private'].

I took out the private to see what happened and put float it said I have no semicolon (;) on movement line and also highlighted void IsGround. I know the syntax is off. Please help me.

(In C# should I put all of my variables at the top of the page? Whereas Java script you can write them anywhere as long as they have var in front of them?)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour 
 {
     //positioning
     public float speed = 2;
     public float distToGround;
     public Transform centerOfMass;
     public Transform cam;
 
     //shooting
     public GameObject shot;
     public Transform shotSpawn;
     public float fireRate;
     private float nextFire;
     public GameObject target;
 
 
     void Start()
     {
         // get the distance to ground
         distToGround = collider.bounds.extents.y;
     }
     
     void IsGrounded()
     {
         return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
     }
     
     void Update ()
     {
         if (Input.GetButton("Fire1") && Time.time > nextFire) 
         {
             nextFire = Time.time + fireRate;
             Instantiate (shot, shotSpawn.position, shotSpawn.rotation); //as GameObject;
             //audio.Play();
             Attack();
         }
     }
     
     private void Attack()
     {
         HealthBar eh = (HealthBar)target.GetComponent("HealthBar");
         eh.AdjustCurrentHealth(-10);
     }
     
     void FixedUpdate () 
         
     {
         //THe directions the player peice will go based the keyboard arrows
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         
         private movement Vector3 = new Vector3 (moveHorizontal,0.0f,moveVertical);
 
         
         
         // When the player peice turns left or right, it tilts slightly
         rigidbody.rotation = Quaternion.Euler (0.0f,rigidbody.velocity.x, 0.0f);
         
         
         // The acceleration for the player peice 
         rigidbody.velocity = movement * speed;
         
         
         // The Camera is following the player peice
         rigidbody.AddForce (transform.rotation * cam.transform.forward * moveVertical);
         rigidbody.AddForce (transform.rotation * cam.transform.right * moveHorizontal);
     }
 }











Comment
Add comment · Show 1
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 gjf · Oct 25, 2014 at 02:47 PM 0
Share

unrelated to the question, this line:

 HealthBar eh = (HealthBar)target.GetComponent("HealthBar");

can be written as:

 HealthBar eh = target.GetComponent<HealthBar>();

it's less likely to cause run-time errors if you change the name of the HealthBar class

it's also good practise to check for null after getting a component (or not...)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Oct 25, 2014 at 12:44 PM

The error is correct, one, private isn't needed whatsoever in a variable that is in the local scope of a method(in this case FixedUpdate.

on line 55 change this:

 private movement Vector3 = new Vector3 (moveHorizontal,0.0f,moveVertical);

to:

 Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical);

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can I make reference/assocation to another component as variable? 0 Answers

Use Of Static Variables 1 Answer

C'# When Taking Variables From Other Scripts, The Values Do Not Update 1 Answer

C# How do I reference a variable from one script in another script 2 Answers

How to call a function from another script - getting error "Object reference not set to an instance of an object" 5 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