Search Unity

Question How do I call and relocate prefabs through code?

Discussion in 'Scripting' started by MaliyaSketuve, Dec 31, 2023.

  1. MaliyaSketuve

    MaliyaSketuve

    Joined:
    Dec 22, 2023
    Posts:
    2
    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,
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,336
    Hi and welcome to the Unity forum :)

    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: https://docs.unity3d.com/Manual/InstantiatingPrefabs.html 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: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.8/manual/Concepts.html

    Best of luck on your game dev journey !!
     
  3. MaliyaSketuve

    MaliyaSketuve

    Joined:
    Dec 22, 2023
    Posts:
    2
    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.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    I wish we could teach ChatGPT to use proper code tags when they regurgitate code fragments.
     
    Anthiese, Nad_B and spiney199 like this.