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
-1
Question by oscarkool · Jan 01, 2014 at 04:16 PM · c#transformvariableprogramming-basics

Converting this JS code to C#

Hello, I have attempted to convert this JS code into C# and I feel that I'm close but not quite there obviously. The idea is to get my player controller to shoot a prefab when I click the mouse button. I've attached the prefab as a projectile. Here's the JS code followed by my C# conversion. I am brand new to coding by the way (just started learning yesterday).

 var projectile : Rigidbody;
 var damage = 5;
 
 function Update () {
     if(Input.GetButtonDown("Fire1")){
         Spawnpoint = transform.Find("First Person Controller/Main Camera/Spawnpoint");
         SpawnVector = Vector3(Spawnpoint.position.x, Spawnpoint.position.y, Spawnpoint.position.z);
         var clone : Rigidbody;
         clone = Instantiate(projectile, SpawnVector, Spawnpoint.rotation);
 
         clone.velocity = Spawnpoint.TransformDirection (SpawnVector.forward*20);
     }
 }

and mine:

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : MonoBehaviour {
 
     private Transform spawnPoint;
     public Rigidbody projectile;
     public int damage = 5;
     
     void  Update (){
         if(Input.GetMouseButtonDown(0)){
             spawnPoint.transform.Find("Player/MainCamera/Spawnpoint");
             Vector3 SpawnVector = new Vector3(spawnPoint.position.x, spawnPoint.position.y, spawnPoint.position.z);
             Rigidbody clone;
             clone = Instantiate(projectile, SpawnVector, spawnPoint.rotation) as Rigidbody;
             clone.velocity = spawnPoint.TransformDirection (Vector3.forward * 20);
         }
     }
 }

Thanks for the help, really pushing myself hard to learn scripting.

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 Sundar · Jan 01, 2014 at 04:59 PM 2
Share

It should be spawnPoint = transform.Find("Player/$$anonymous$$ainCamera/Spawnpoint");

Not spawnPoint.transform.Find("Player/$$anonymous$$ainCamera/Spawnpoint");

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by DanTup · Jan 01, 2014 at 05:56 PM

There are a few differences in the two scripts posted:

  • Input.GetButtonDown("Fire1") vs Input.GetMouseButtonDown(0)

  • Spawnpoint = transform.Find vs spawnPoint.transform.Find

  • "First Person Controller/Main Camera/Spawnpoint" vs "Player/MainCamera/Spawnpoint"

The first and last difference might be deliberate, but the middle one looks certainly wrong. In the JavaScript, it's creating a variable that stores the spawnpoint that it finds, to then use on the next line. In the C# version, the result ot the Find method is discarded; so I suspect you want to change:

 spawnPoint.transform.Find("Player/MainCamera/Spawnpoint");

to

 spawnPoint = transform.Find("Player/MainCamera/Spawnpoint");

This will call the Find method on the local objects transform, and assign it to the spawnPoint variable, which is then used in the next line. However, spawnPoint is likely null and will result in a NullReferenceException (which would cause the rest of the Update method to be skipped).

Comment
Add comment · Show 7 · 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 DanTup · Jan 01, 2014 at 06:37 PM 0
Share

Downvote?! Classy. It's like StackOverflow all over again :O(

avatar image AlucardJay · Jan 01, 2014 at 11:00 PM 0
Share

WTF, this doesn't deserve a downvote (unless the original was much different to the edit)

First point is irrelevant. This is just using a button defined in the Input $$anonymous$$anager vs a specific input event.

Second point is spot on, same as the comment that received 2 votes, and you have given formatted example code.

Last point is a good consideration if the OP hadn't renamed the First Person Controller, na$$anonymous$$g, tags, and layers being common 'gotchas'

Upvoted. I would ignore downvotes, serious people usually leave a comment as to why (unless it's blatantly obvious why), or even leave comments on what is wrong and the correct methods to the incorrect suggestions. Hope your next experience on UnityAnswers is better than this.

avatar image landon912 · Jan 02, 2014 at 02:48 AM 0
Share

Up voted to void whoever down voted, cause it's a dang good answer ;)

avatar image oscarkool · Jan 02, 2014 at 03:08 AM 0
Share

Thanks for the help! I would upvote if I could but I have to have 15 rep and I just joined.

I'm only getting 1 error now and it occurs when I click the mouse. It says "NullReferenceException: Object reference not set to an instance of an object Projectile.Update () (at Assets/Scripts/Projectile.cs:13)" and its occuring on the Vector3 line.

 using UnityEngine;
 using System.Collections;
  
 public class Projectile : $$anonymous$$onoBehaviour {
  
     private Transform spawnPoint;
     public Rigidbody projectile;
     public int damage = 5;
  
     void  Update (){
        if(Input.Get$$anonymous$$ouseButtonDown(0)){
          spawnPoint = transform.Find("First Person Controller/$$anonymous$$ain Camera/Spawnpoint");
          Vector3 SpawnVector = new Vector3(spawnPoint.position.x, spawnPoint.position.y, spawnPoint.position.z);
          Rigidbody clone;
          clone = Instantiate(projectile, SpawnVector, spawnPoint.rotation) as Rigidbody;
          clone.velocity = spawnPoint.TransformDirection (Vector3.forward * 200);
        }
     }
 }
avatar image landon912 · Jan 02, 2014 at 03:13 AM 0
Share

Spawn point isn't getting set as anything. transform.Find isn't finding anything, check to make sure everything matches up.

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

22 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

Related Questions

Use of unassigned local variable `selection' Destroying instance 1 Answer

How to set a Transform variable with code (C#) 3 Answers

How to assign a private transform of my player in script? 1 Answer

Variable values in Variable names 2 Answers

Accessing a variable from js with c# script 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