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

error trying to copy PropertyInfo. Non-static method requires a target.

Discussion in 'Scripting' started by jister, May 13, 2014.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    this worked like a charm before? and i don't see why it doesn't anymore ...

    Code (csharp):
    1. public void PickUp(Item item)
    2.     {
    3.         Item i = new Component() as Item;
    4.         BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
    5.         foreach (PropertyInfo f in item.GetType().GetProperties(flags))
    6.         {
    7.             Debug.Log(f.PropertyType.Name);
    8.             if(f.PropertyType == typeof(Transform))
    9.                 break;
    10.             f.SetValue(i, f.GetValue(item, null), null);
    11.         }
    12.         Inventory.itemList.Add(i);
    13.         Destroy(item.gameObject);
    14.     }
    error:
    Code (csharp):
    1. TargetException: Non-static method requires a target.
    2. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:236)
    3. System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoProperty.cs:348)
    4. System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/PropertyInfo.cs:102)
    5. PlayerCharacter.PickUp (.Item item) (at Assets/Scripts/CharacterScripts/PlayerCharacter.cs:69)
    6. PlayerCharacter.LateUpdate () (at Assets/Scripts/CharacterScripts/PlayerCharacter.cs:57)
     
  2. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    Most likely a null reference. I had a problem too with static and non static classes as well. Is the Item class static or any class your using static?
     
  3. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    Code (csharp):
    1. Item i = new Component() as Item;
    will always return null, if Component do not extend Item
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    @DexRobinson: nope no static classes in use.
    @PrefabEvolution: i swear i had it working like this before... is there an other way to create an instance of a class inheriting from monobehaviour?
     
  5. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    Code (csharp):
    1. Item i = this.AddComponent(item.GetType()) as Item
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    but all i want to do is copy the values to a new instance for a list in the inventory. i don't have a gameobject to put it on.
    guess i can't have a instance of a component without a gameobject in the scene for it to sit on. bummer. back to ScriptableObject then.