Hey boys n girls!
Ive recently tried switching to C# since I want to learn that aswell, and here I am with problems already 
Im trying to do a Raycast from the main cameras transform along the main cameras Vector3.forward. I then want to instantiate a prefab at the point where the ray intersects anything when the user clicks the mouse.
But im getting errors and its probably related to how I declare variables I think. anyway, heres the code.
public GameObject turret = new GameObject(); // the gameobject I want to instantiate
public Transform _position = new Transform(); // the variable to store the position of the raycast
void onPlacement(){
if (Input.GetButtonDown("Fire1")){
RaycastHit hit;
if (Physics.Raycast (Camera.main.transform.position,Camera.main.transform.forward, out hit)) {
_position = hit.transform.position;
turret = Instantiate(turret, _position, Quaternion.identity);
}
}
}
now I know this is wrong. In javascript I would simply declare a new variable like so
var shoot = Instantiate(turret, _position, Quaternion.identity);
But thats not doable in C# since I need to declare a type, right? I have no idea what the type of “shoot” is. Any help to what I should correct here?
Always post the errors, otherwise we’re left guessing what your problem is. Here are some pointers:
You shouldn’t be using ‘= new GameObject()’ or ‘= new Transform()’ to initialize your public variables. You’ve declared them public so you can set them in the inspector. (On a side note, you probably don’t want to set _position through the inspector, so you can make that private instead of public.)
Your Instantiate() call is assigning the result to the ‘turret’ variable you use to hold a reference to your prefab. That means, next time you call this function, it’s going to try to instantiate from that instead of the prefab as you intend.
If you need to keep a reference to the instantiated object, store it in a seperate variable (either a class-level variable, if you need on-going access to the object, or a local if you just need it withing onPlacement()).
You can check the Scripting Reference documentation to find out what any function returns.
GameObject.Instantiate
In this case: UnityEngine.Object. More specifically, it will return a clone of what you pass in as ‘original’, so here you can safely cast to GameObject.
If that doesn’t help, post a reply and remember to include the specific errors 
Alright so based on your reply I did changed my code to the following instead:
public GameObject turret; // the gameobject I want to instantiate
Vector3 _position; // the variable to store the position of the raycast
void onPlacement(){
if (Input.GetButtonDown("Fire1")){
RaycastHit hit;
if (Physics.Raycast (Camera.main.transform.position,Camera.main.transform.forward, out hit)) {
_position = hit.transform.position;
GameObject tower;
tower = Instantiate(turret, _position, Quaternion.identity);
}
}
}
Im not sure Im reading your post correctly though. I dont need to keep reference once the prefab has been instantiated, it should run on its own from there on out.
I get this error at line 39, which is : tower = Instantiate(turret, _position, Quaternion.identity);
Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)
Maybe you already answered this and im just having too little focus right now to see it 
EDIT: I changed line 38 to :
Object tower;
Now I succesfully instantiate my object (which is just a cube atm), but it is instantiated right on top of my player character. since this is a third person perspective, its likely because the ray shoots right into the back of my player
gonna have to think of a fix for that. but thanks for your help
I was wrong. My instantiation always happens at exactly the same spot, regardless of where the camera is pointing when I click the mouse. I must be doing something wrong here…
Check what the ray is hitting? (try print(hit.transform.name)
If you want the raycast to ignore certain objects, place them in a separate layer and pass a LayerMask to Physics.Raycast() to exclude that layer.
Yeah I was being an idiot. I instantiated my prefab using hit.transform.position. This is obviously the position of the transform that the ray hit.
I should obviously be using hit.point which is the point in space where the ray intersected. Silly me, thats what you get for sitting up late 