- Home /
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.
It should be spawnPoint = transform.Find("Player/$$anonymous$$ainCamera/Spawnpoint");
Not spawnPoint.transform.Find("Player/$$anonymous$$ainCamera/Spawnpoint");
Answer by DanTup · Jan 01, 2014 at 05:56 PM
There are a few differences in the two scripts posted:
Input.GetButtonDown("Fire1")
vsInput.GetMouseButtonDown(0)
Spawnpoint = transform.Find
vsspawnPoint.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).
Downvote?! Classy. It's like StackOverflow all over again :O(
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.
Up voted to void whoever down voted, cause it's a dang good answer ;)
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);
}
}
}
Spawn point isn't getting set as anything. transform.Find isn't finding anything, check to make sure everything matches up.