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

You are not allowed to call this function when declaring a variable

Discussion in 'Scripting' started by hexicCore, Jun 16, 2014.

  1. hexicCore

    hexicCore

    Joined:
    May 4, 2014
    Posts:
    11
    Code (CSharp):
    1. PlacedObj = (GameObject)Instantiate(Resources.Load("target"));
    For some reason this line generates the error "You are not allowed to call this function when declaring a variable". I have tried breaking it down to this:

    Code (CSharp):
    1. Object LoadPrefab = Resources.Load("target");
    2. Object CreatedObj = new Object();
    3. CreatedObj = Instantiate(LoadPrefab);
    4. PlacedObj = (GameObject)CreatedObj;
    I still get the error on the Instantiate line. I read that you need to wait for the object to Awake(), but how would I do that in this context?
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    you cannot call these functions outside of a function. So you need to do this in either the Awake function or in the Start function.

    eg:
    Code (csharp):
    1. GameObject LoadPrefab;
    2. GameObject CreatedObj;
    3.  
    4. void Awake() { //or Start()
    5.   LoadPrefab = Resources.Load("target");
    6.   CreatedObj = Instantiate(LoadPrefab) as GameObject;
    7. }
     
  3. hexicCore

    hexicCore

    Joined:
    May 4, 2014
    Posts:
    11
    These are called in the Update() function when a key is pressed. And I am getting this message long after the object has been created.
     
  4. ElvisAlistar

    ElvisAlistar

    Unity Technologies

    Joined:
    Oct 2, 2013
    Posts:
    226
    Try
    Code (csharp):
    1. GameObject CreatedObject = Instantiate(Resources.Load("target", typeof(GameObject)));
     
  5. hexicCore

    hexicCore

    Joined:
    May 4, 2014
    Posts:
    11
    Returned error: "Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.GameObject'".
     
  6. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    I think your problem is that the variable PlaceObject has not been declared yet. Either declare it first before assigning it a value, or like ElvisAlistar has already suggested, declare it at the same time that you are assigning the value.
     
  7. ElvisAlistar

    ElvisAlistar

    Unity Technologies

    Joined:
    Oct 2, 2013
    Posts:
    226
    What type of asset is "target"? Is it a prefab? Is it something else?
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    @ElvisAlistar
    Instantiate returns an object, rather than a gameObject.
    So you'd have to cast it to the type GameObject :'P

    @hexicCore
    This is how u could do it, assuming that "target" is of the type GameObject
    Code (CSharp):
    1. GameObject createdObject = (GameObject)Instantiate((GameObject)Resources.Load("target"));
    To make it look cleaner:
    Code (CSharp):
    1. GameObject targetPrefab = (GameObject)Resources.Load("target");
    2. GameObject createdObject = (GameObject)Instantiate(targetPrefab);
    or
    Code (CSharp):
    1. GameObject targetPrefab = Resources.Load("target") as GameObject;
    2. GameObject createdObject = Instantiate(targetPrefab) as GameObject;
    3.  
     
    Last edited: Jun 18, 2014
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    So you need an explicit conversion. Either
    Code (csharp):
    1. (DesiredType)blah
    ...or...
    Code (csharp):
    1. blah as DesiredType