How do I call and relocate prefabs through code?

Hello,
I hope my questions will be clear enough so that you will be able to tell me which resources/code models I can use.
I am sorry for I am not able to shorten my explanations.

I am new to game programming, and since I am creating a snow sports game (at least just snow for now) I need your help or at least guiding on how to.

I have looked in the documentation but have not been able to find any info about how I could call a prefab through c# code. I know how to use my prefab directly in my scene in the unity editor, but since there are a lot of occurences of my prefab in the map, it would be HDD space and developper time consuming to work this way.

My current situation

  • I have created my skying ground and basic visuals.
  • I want to inject X times the slalom doors (my prefabs) in my scene at coordinates referenced in a csv file (I know how to manage the file since I was previously a management software developer).

Since there are several levels (debutant, intermediate, advanced, expert) for each type of slalom (slalom, superG, …) and each ski slope, I would prefer if I could use an already available code to call my prefabs than just relocate after placing those manually.

The steps I plan to use :
on load, the gamer will have to choose which slope, game mode and difficulty, then the game will have to load the scene matching the gamer’s choices.
in the start script of the scene, it would have to load the number of doors to go through, the model and location of the prefabs and set the various occurrences in the right place using the coordinates and rotation info from the matching csv file.

Question 1:
which instruction reference should I include through the using call.
Question 2 :
could you give me a link to the right chapter so that I could look more precisely into what I need., or if there isn’t a chapter could you give me an example of code ?

Question 3:
since there is a getaxis, there should be a setaxis too, am I right?

Thank you for the time you took reading my request.
Best regards,

Hi and welcome to the Unity forum :slight_smile:

Q1: which instruction reference should I include through the using call.
I did not quite get what you mean? Sorry.

Q2: could you give me a link to the right chapter …
The process of placing prefabs in a scene by code is called “instantiating a prefab”. Manual: Unity - Manual: Instantiating Prefabs at run time but there are also a lot of tutorials out there. Simply search for it.

Q3: since there is a getaxis, there should be a setaxis too, am I right?
If by “GetAxis” you mean Input.GetAxis() then no there isn’t. Also you shouldn’t use that anymore since that’s the old input system. If you are starting off anew better use the new input system right from the start. - To help you out while seaching: anything that starts with “Input.” is the old input system. Anything that talks about input actions is the new system. I’ve found this page to be very helpful in understanding it all: Basic Concepts | Input System | 1.8.2

Best of luck on your game dev journey !!

Hello,
Thank you marylisaa32. Your explanation is cristal clear. Now, I understand how to use the Instantiate().
That will help me a lot alongside with what geo sent me.
Have a good day.

I wish we could teach ChatGPT to use proper code tags when they regurgitate code fragments.

3 Likes