Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 jhsu · Jul 14, 2015 at 12:08 AM · variable-definitionjs to c#

Converting JS to C# Issue

I'm trying to convert a prefab shoot script from JS to C# and am getting errors I don't know how to solve:

 // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
 // Do test the code! You usually need to change a few small bits.
 
 using UnityEngine;
 using System.Collections;
 
 public class PrefabShooting : MonoBehaviour {
     
     Vector3 torque;
     Rigidbody theBullet;
     int Speed = 20;//<- FIXME_VAR_TYPE
     int rndMag = 200;//<- FIXME_VAR_TYPE
     void  Update (){
         
         if (Input.GetMouseButtonDown(0)){
             var clone = Instantiate(theBullet, transform.position, transform.rotation);//<- FIXME_VAR_TYPE
             Vector3 camDir = Camera.main.transform.forward;//<- FIXME_VAR_TYPE
             clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
             
             // ADD RANDOM TORQUE
             torque.x = Random.Range (rndMag*-1, rndMag);
             torque.y = Random.Range (rndMag*-1, rndMag);
             torque.z = Random.Range (rndMag*-1, rndMag);
             clone.AddTorque(torque);
             
             Destroy (clone.gameObject, 30);
         }
     }
     
     
 }

The FIXME_VAR_TYPE commented lines are where I just came up with my best guess for variable types. I saw someone mentioning using "Debug.Log(myVariable.GetType() );" in another post, but I don't understand how I'm supposed to use that if the variable creates a compile error and prevents you from running the script in the first place.

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 InfiniBuzz · Jul 14, 2015 at 12:14 AM 0
Share

Provide console output and more information about your error, please.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DiegoSLTS · Jul 14, 2015 at 12:36 AM

Speed should be a float if you'll use it to create a Vector3.

rndMag should be a float too, unless you actually want to use the Random.Range function that returns an int value (doesn't look like that if you assign it to the components of a Vector3).

"clone" could be a Rigidbody since you're instantiating a new object from "theBullet", which is a RigidBody. You can cast the value returned by instantiate or you can use the generics version:

 Rigidbody clone = Instantiate<Rigidbody>(theBullet, transform.position, transform.rotation);

camDir is a Vector3, that's OK.

And this is not a type issue, it's not even an issue, but instead of:

 Random.Range (rndMag*-1, rndMag)

it would be easier to read if you write:

 Random.Range (-rndMag, rndMag);
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 jhsu · Jul 20, 2015 at 03:47 PM 0
Share

Thanks for the input, very helpful

avatar image
1

Answer by maccabbe · Jul 14, 2015 at 12:28 AM

1 - avoid using variables of type 'var' (line 16)

2 - declare new objects using new keyword (line 18)

The following compiles but you should deal with the warnings. As the warnings should indicate, you never assign anything to theBullet and it can't be assigned from the outside as it is not public (line 10). Also camDir is assigned but never used (line 17).

 using UnityEngine;
 using System.Collections;
 
 public class PrefabShooting : MonoBehaviour {
     
     Vector3 torque;
     Rigidbody theBullet;
     int Speed = 20;//<- FIXME_VAR_TYPE
     int rndMag = 200;//<- FIXME_VAR_TYPE
     void  Update (){
         
         if (Input.GetMouseButtonDown(0)){
             Rigidbody clone = Instantiate(theBullet, transform.position, transform.rotation) as Rigidbody;//<- FIXME_VAR_TYPE
             Vector3 camDir = Camera.main.transform.forward;//<- FIXME_VAR_TYPE
             clone.velocity = transform.TransformDirection(new Vector3(0, 0, Speed));
             
             // ADD RANDOM TORQUE
             torque.x = Random.Range (rndMag*-1, rndMag);
             torque.y = Random.Range (rndMag*-1, rndMag);
             torque.z = Random.Range (rndMag*-1, rndMag);
             clone.AddTorque(torque);
             
             Destroy (clone.gameObject, 30);
         }
     }    
 }
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 jhsu · Jul 20, 2015 at 03:48 PM 0
Share

I'll give it another go, thanks!

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

How do I get the Year I built my game in? 0 Answers

JS Conversion to C# - ToBuiltin? Unity 3 Game Development Hotshot from Jate Wittayabundit 1 Answer

Invalid Layer Index on mecanim animator 4 Answers

Unable to disable a C# script with a JS Script 2 Answers

Creating single variable across all app instances 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