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. Dismiss Notice

Instantiate a blank Gameobject

Discussion in 'Scripting' started by Shadowing, Feb 15, 2015.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    I'm getting hit with an error when I Instantiate a blank Gameobject
    I swear this is what the manual says to do.
    Except they aren't passing a string for the prefab name.
    I have the prefab in my resources folder too.

    The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments

    Code (csharp):
    1.  
    2.  
    3. GameObject newButton = Instantiate ("CharacterSelectButton",Vector3.zero, Quaternion.identity) as GameObject;
    4.  
    5.  
    I tried creating a variable like this so I could drag the prefab in
    public GameObject CharacterSelectButton;

    but when I do that it doesn't show up as a slot on the Game object this script is attached too
    So I don't understand that either.
     
    Last edited: Feb 15, 2015
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    thanks for the response hpjohn
    I figured some things out.

    If you don't assign
    Code (csharp):
    1. public GameObject CharacterSelectButton;
    to anything it won't show up as a slot to drag the CharacterSelectButton prefab in.
    So that explains why some times public variables show up for me and other times they haven't

    Apparently when im using PhotonNetwork.Instantiate I can pass a string but I guess we cant with just using Instantiate.
     
    Last edited: Feb 15, 2015
  4. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I am not 100% sure but try this:
    Code (CSharp):
    1. GameObject newButton = Instantiate ("CharacterSelectButton",Vector3.zero, Quaternion.identity);
    I don't think you need the
    Code (CSharp):
    1. as GameObject
     
  5. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    ya I was wondering if I needed the as GameObject too
    if I remove it I get error
    Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'.

    Code (csharp):
    1. GameObject NewCharacterSelectButton = Instantiate (characterSelectButton) as GameObject;
    2. NewCharacterSelectButton.Find("Name").GetComponent <Text> ().text = name;
    3. NewCharacterSelectButton.Find("Level").GetComponent <Text> ().text = level;
    Getting hit with error
    Static member `UnityEngine.GameObject.Find(string)' cannot be accessed with an instance reference, qualify it with a type name instead

    So im guess its cause NewCharacterSelectButton is an instance.
    "qualify it with a type name instead"
    How do I do this?

    thanks for the responses btw guys.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Find is not a member of a gameobject instance, its a static
    I thought your problem was solved, 420blazeits post wasn't relevant or correct
     
  7. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    Thanks hpjohn
    Ya I got it solved. I created another class added it to the prefab so it could find those components on each instance.
     
  8. 00yoshi

    00yoshi

    Joined:
    Jan 24, 2015
    Posts:
    16
    Try changing Vector3.Zero to new Vector3(0.5,0.5,0.5)