3

The problem I have is that CreateInstance returns null.

Here is the code:

if(spattmono[0] != null)
{
    if((SpecialAttack) System.Activator.CreateInstance(
        spattmono[0].GetClass()) == null)
    {
    Debug.Log("DUMB ACTIVATOR!!!");
    }

//combo.SetSpecialAttack(spattack);
}

Attack and SpecialAttack are both classes that store basic information, and inherit from UnityEngine.Object.

Attmono and spattmono are both MonoScript arrays, attmono being able to hold 16 and spattmono being able to hold 4.

They get there information from these.

for(int at = 0; at < numberOfAttacks; ++at )
{
    attmono[at] = (MonoScript) EditorGUILayout.ObjectField(attmono[at], 
        typeof(MonoScript), false);
}

for(int spat = 0; spat < 4; ++spat )
{
    spattmono[spat] = (MonoScript) EditorGUILayout.ObjectField(
        spattmono[spat], typeof(MonoScript), false);
}

You could think of MonoScript just as something that holds what class type the object is. I have checked each of these with Debug.Print statements and both are not null when being assigned.

Here is the SpecialAttack code.

public class SpecialAttack : UnityEngine.Object 
{
    public string Name;

public int Damage;
public int Force;
public float Cooldown;

public SpecialAttack() 
    { }

public virtual bool Run() 
    {
    return false;
}
}

Ive recently tested this

if((SpecialAttack)System.Activator.CreateInstance(spattack.GetType()) == null) 
{
    Debug.Log("DUMB ACTIVATOR!!!");
}

And it was indeed null, so that makes me believe that the Activator cant find the type, so im not to sure what to do from here.

Diddykonga
  • 31
  • 3
  • What are `Attack`, `SpecialAttack`? What's in the `spattmono[]` and `attmono[]` arrays, how are you creating them. You need to expand this question somewhat for anyone to be able to have a reasonable chance at answering this. – Kev Aug 21 '11 at 11:18
  • what does `spattmono[0].GetClass())` return when the CreateInstance returns null? – Rune FS Aug 21 '11 at 11:55
  • It returns a System.Type, oh and the second CreateInstace i posted before i edited my post, it was also returning null :( – Diddykonga Aug 21 '11 at 12:07
  • Does SpecialAttack have publicly accessible constructor? Ms.NET in this case throws ArgumentException, but Mono (UnityEngine) possible return null. – arbiter Aug 21 '11 at 14:46
  • Still need help if anyone happens to have the answer to this, thanks! :) – Diddykonga Aug 21 '11 at 22:58
  • @Diddy, What _value_ does the above mentioned method call return? – Rune FS Aug 22 '11 at 08:05

1 Answers1

0

Let's assume that Activator.CreateInstance does actually work correctly, then there must be sometihng wrong with the arguments supplied to the method call. (Hence the question earlier).

Due to lack of knowledge of the actual value passed to CreateInstance I'm guessing but I would suspect the value passed being null or being a type that is either internal but resides in a different assembly or similar access problems.

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • Well the MonoScript that i pass into the Activator is not null, becuase i have a check for that before i try. About the access problem im not sure. – Diddykonga Aug 22 '11 at 11:57
  • The MonoScript? you said GetClass() returned a Type which one is it? – Rune FS Aug 22 '11 at 13:07
  • What? it does return the Type. I was saying that the MonoScript variable spattmono[0] cant be null because i have a check before i use CreateInstance. – Diddykonga Aug 22 '11 at 23:36
  • Above you Said "the MonoScript I pass" but you're not passing a MonoScript but the value returned from GetClass(). If the MonoScript was null you'd have an exception. I'm thinking what ever GetClass is returning is not what you expect it would be – Rune FS Aug 23 '11 at 07:02
  • Well ive also checked what GetClass() returns also, cause i too was skeptikul. If i run this Debug.Log(spattmono[0].GetClass().ToString()); I recieve SpecialAttack printed out. – Diddykonga Aug 23 '11 at 11:22
  • Thought i would just go ahead and mention that GetClass() returns a System.Type, and i have also sent an email to Unity's support email about this issue, becuase i really have no idea how they store the compiled files and where. – Diddykonga Aug 23 '11 at 11:50
  • @diddy what happens if you inatead og ....GetClass() writes typeof(SpecialAttack). If it doesn't compile then figure out why – Rune FS Aug 24 '11 at 21:13