Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I'm slowly getting crazy... I just need to instantiate ONE sprite in a script (at runtime)

Discussion in '2D' started by ___Marc___, Feb 21, 2015.

  1. ___Marc___

    ___Marc___

    Joined:
    Feb 21, 2015
    Posts:
    2
    Hi folks,
    I'm trying to do something dead simple.
    I created a prefab based on an image of a card. Created an empty prefab. Imported the image in the assets. Dragged the image on the empty prefab, and voila, here's the "prefab" (I see it more os a sprite, actually, but OK).

    The prefab's name is prfCard. I've put it in the Resources folder.

    I need to do ONE thing.
    1. Start with an empty screen.
    2. When I click on left mouse button, I need to instantiate the prefab (or the image, I don't care), in the middle of the screen.

    That's it, no more. If the "click on the left mouse button is too complicated", I just need a script (Javascript or c#, don't care) to INSTANTIATE the prefab (or image) in the middle of the screen.

    The code at http://docs.unity3d.com/Manual/InstantiatingPrefabs.html generated gazillion error messages.

    I just need to instantiate a sprite. That's all folks. Can't someone explain to me (baby-style) what code to write (Javascript would be great, but I will survive with C#) just to instantiate a sprite.

    More importantly, where must I put this code ? In a script that is embedded in another prefab ? Must this "another prefab" already be on the screen ? How can I run code if the screen is completely empty ?

    I can put another prefab on the screen with the picture of a rectangle, if needed, but this is explained nowhere.

    How to make this amazanginly simple task ?

    Help. My sanity is falling !
     
  2. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    In C#:

    Set the preFab and position. Set position to whatever you consider the "middle of the screen":

    Code (CSharp):
    1. public GameObject card;
    2. private Vector3 position;
    3. private bool spawned;
    4.  
    5. void Start(){
    6.  
    7. position = new Vector3(0,0,0);
    8. spawned = false;
    9.  
    10. }
    Instantiate it (Assuming "Fire" is set as your moue button in the Input manager):

    Code (CSharp):
    1. void Update(){
    2.  
    3. if(Input.GetButtonDown("Fire") && spawned == false){
    4.  
    5. Instantiate (card, position,Quaternion.identity);
    6. spawned = true;
    7.  
    8. }
    9. }
    Put the script on a GameObject (or the camera?) and drag your prefab onto the "card" GameObject in the inspector.
     
  3. ___Marc___

    ___Marc___

    Joined:
    Feb 21, 2015
    Posts:
    2
    Hi Senladar,
    thank you so much for your reply. Really appreciated.

    First thing. I'm having several "a-ha moments" just by looking at your code. For example: putting the script on the camera. The camera is always there. Actually, as I see it, it's the best place to put the script where you initialize your variables, where to create global variables, etc... am I wrong ?
    The "starting tricks" are not clearly explained in the documentation. They assume you have other objects in the scene.

    When I copy / pasted the code *inside the component*, it told me *in an infinite loop* that
    "
    UnityException: Input Button Fire is not setup.
    To change the input settings use: Edit -> Project Settings -> Input
    Instantiate.Update () (at Assets/CS/Instantiate.cs:23)
    "

    So, I went to the InputManager, and saw that I must use "Fire1" instead of "Fire". When I replaced "Fire" to "Fire1", same result.

    So, I replaced "Fire" with "Jump". It instantiates the prefab when I hit the spacebar.

    Is there some spiel I must do in the input manager to link the identificator "Fire" to the left mouse button ?

    There is another thing bugging me a bit. I found out that I must *manually* link the card variable in your script with the prefab in the Visual IDE. What should we do if we want this step to happen automatically. I have a prefab called prfCard.

    What to write in the code so that the card variable is set to prfCard without having to do it manually. Imagine I have 52 cards, and I want some logic to instantiate the card...

    Thank you. The thing is almost done. Again, many thanks.

    Marc.
     
    Last edited: Feb 23, 2015
  4. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    Are you sure it's "Fire" and not "fire"? Make sure there's no space as well "Fire ". Alternately you can use:

    Code (CSharp):
    1. Input.GetMouseButtonDown(0);
    "0" being the left button:

    http://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html

    You can load it directly from the script by putting the prefabs into a folder called "Resources" at the base level, then putting all of the cards into a folder called "Cards" under that. If you want to have multiple, I recommend using a List by adding the following at the top of your code:

    Code (CSharp):
    1. using System.Collections.Generic;
    Then replace your public GameObject card with:

    Code (CSharp):
    1. public List<GameObject> card = new List<card>();
    2.  
    3.  
    In Start, add:

    Code (CSharp):
    1. foreach(GameObject g in Resources.LoadAll("Cards", typeof(GameObject))
    2. {
    3. card.Add(g);
    4. }
    Then you can replace "card" in Instantiate with "card[Random.Range(0,card.Length)]" to load a random card from your bucket:

    Code (CSharp):
    1. Instantiate (card[Random.Range(0,card.Length)], position,Quaternion.identity);
    :)