Hello guys,
how can i transform this JavaScript:
var bullit = Instantiate(bullitPrefab, GameObject.Find("spawnPoint").transform.position, Quanterion.identity);
bullit.rigibody.AddForce(transform.format * 200);
to C#.
Hello guys,
how can i transform this JavaScript:
var bullit = Instantiate(bullitPrefab, GameObject.Find("spawnPoint").transform.position, Quanterion.identity);
bullit.rigibody.AddForce(transform.format * 200);
to C#.
using UnityEngine;
using System.Collections;
public class MYCLASSNAME : MonoBehaviour {
GameObject bullit= Instantiate(bullitPrefab, GameObject.Find("spawnPoint").transform.position, Quanterion.identity);
bullit.rigibody.AddForce(transform.format * 200);
}
i have do this, but the class GameObject can not us Instantiate and with the Object class cant us rigibody.
cast from object to gameobject build a error.
GameObject change that to public something…
You would need to put the instantiate in the Update() or Start function of the monobehaviour class, like this:
using UnityEngine;
using System.Collections;
public class MYCLASSNAME : MonoBehaviour {
public GameObject bulletPrefab;
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quanterion.identity);
bullet .rigibody.AddForce(transform.format * 200);
Destroy(bullet, 3f); //Delete bullet after 3 seconds
}
}
}
Drag the script onto your player or any game object in the scene, then drag the bullet prefab on the public variable ‘bulletPrefab’, run the game and hitting space will instantiate you a bullet!
If you’re converting a lot JS to C#, our script could help save you some time: Convert unity javascript (unityscript) to C#
I guess it’s better to understand your code than to trust it?
Yeah, but me question is the Type of Bullet. you don’t write a type.
thanking you in anticipation
I’m sorry, I indeed made some mistakes… Here is the updated version:
using UnityEngine;
using System.Collections;
public class MYCLASSNAME : MonoBehaviour {
public GameObject bulletPrefab;
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
GameObject bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quanterion.identity);
bullet.rigibody.AddForce(transform.format * 200);
Destroy(bullet, 3f); //Delete bullet after 3 seconds
}
}
}
The instantiated bullet is a GameObject. ![]()
//Change
GameObject bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quanterion.identity);
//to
GameObject bullet = (GameObject)Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quanterion.identity);
Casting is easy, just put ([type]) in front of what you want to cast, Mkay?
i know and i do this before i write here.
all there build a error:
now:
InvalidCastException: Cannot cast from source type to destination type.
Move.Update () (at Assets\Player\Scripts\Move.cs:35)
and in C#:
Move.cs(38,20): error CS1061: ‘UnityEngine.GameObject’ does not contain a definition for ‘rigibody’ and no extension method ‘rigibody’ accepting a first argument of type ‘UnityEngine.GameObject’ could be found (are you missing a using directive or an assembly reference?)
There were some typos.
Try this:
using UnityEngine;
using System.Collections;
public class MYCLASSNAME : MonoBehaviour {
public GameObject bulletPrefab;
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
GameObject bullet = (GameObject)Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 200);
Destroy(bullet, 3f); //Delete bullet after 3 seconds
}
}
}
When it logs an error, look at the line and see it something is spelled wrong first like rigidbody or Quaternion. It can save you a lot of time.