Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Odd error: Invalid IL Code

Discussion in 'Editor & General Support' started by zumwalt, May 7, 2007.

  1. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Ok, I have a global Gameobject which is my active player.
    This gets populated on startup with the last player resource in the stack.
    My placeLife() code runs fine, moves that ship into play.
    The movePlayer() code executes during the run loop which generates the error of:

    InvalidProgramException: Invalid IL code in SetupObjects:movePlayer() : IL_0023: stloc.1

    Here is the code for the two methods, remember, placeLife() works:

    Code (csharp):
    1.  
    2.  
    3.     void placeLife()
    4.     {
    5.         activePlayer=GameObject.Find("pGun(Clone)");
    6.         Vector3 pPos;
    7.         pPos=activePlayer.transform.position;
    8.         pPos[0]=-120;
    9.         pPos[1]=-85;
    10.         activePlayer.transform.position=pPos;
    11.     }
    12.    
    13.     void movePlayer()
    14.     {
    15.         Vector3 pPos;
    16.         pPos=activePlayer.transform.position;
    17.            
    18.         if (Input.GetKey("a"))
    19.         {
    20.             pPos[0] -= 20 * Time.deltaTime;
    21.             activePlayer.transform.position=pPos;
    22.         }
    23.    
    24.         if (Input.GetKey("d"))
    25.         {
    26.             pPos[0] += 20 * Time.deltaTime;
    27.             activePlayer.transform.position=pPos;
    28.         }
    29.     }
    30.  
    31.  
     

    Attached Files:

  2. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    If I'm not wrong, it sounds like a Mono error.

    I tried compiling your code - i changed "activePlayer." to "this." and it worked ok. Might be worth looking at other parts of the script to make sure your not doing something strange like duplication or reserved words etc

    Cheers
    Shaun
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    From the Mono docs:
    "Note: This exception is thrown by the system when a compiler emits incorrect IL or metadata."

    So something in your code probably screwed it up like Shaun said, but the fact that the bug/error isn't caught in a nicer way is probably Unity's fault.

    -Jeremy
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Are you still using that compiled DLL for SOAP interaction?
     
  5. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    We programmers don't interact with SOAP. Or sunlight.

    -Jeremy
     
  6. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    @jeremy - rofl
     
  7. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Nope, not with this project, that code is working in another project, am at work now, so I can't test the this. reference pointer, will have to check that tonight, doesn't make much sense as to why I would need it though since the method is local and I am not using it from another class.

    Besides, that dll I created does way more than just SOAP, it slices, dices and julians.
     
  8. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    I should clarify - i just used the this. command to test your code, since I don't have the other object in my project. It compiled OK with that - which suggests it something to do with that object you're trying to reference or something freaky elsewhere in your code.

    Cheers
    Shaun
     
  9. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Its specific to the movent code, so maybe putting

    pPos=this.activePlayer.transform.position;
    Might fix the problem, don't know yet.
    But looking at the code, that shouldn't be the case.
    It should just work, like the other method that puts the player onto the playing field.

    Edit:
    To clarify, the error doesn't happen when it compiles, it happens when the game runs.
     
  10. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Found the problem, don't know the solution yet, this is guaranteed to crash my game:

    Code (csharp):
    1.  
    2.         pPos=activePlayer.transform.position;
    3.         pPos[0] -= 20 * Time.deltaTime;
    4.  
    pPos[0] = -120 initially, so trying to do the -= 20* Time.deltaTime crashes the game hard.
     
  11. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Oddly enough, this code works:

    Code (csharp):
    1.  
    2.         pPos=activePlayer.transform.position;
    3.     pPos[0] =pPos[0]- (20 * Time.deltaTime);
    4.     Debug.Log(pPos[0]);
    5.  
    Which is the same thing as

    Code (csharp):
    1.  
    2.         pPos=activePlayer.transform.position;
    3.     pPos[0] -=20 * Time.deltaTime;
    4.     Debug.Log(pPos[0]);
    5.  
    However, it doesn't like the short hand way.
     
  12. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Probably just a style question--but why aren't you using pPos.x = instead of pPos[0] = ? Avoid obscurity!
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's weird...no crashes when I try that here. For what you're doing, you don't really need a vector3 though. Here's my code for that routine:

    Code (csharp):
    1.  
    2. var playerPosition : float = player.transform.position.x + (Input.GetAxis("Horizontal") * Time.deltaTime * pSpeed);
    3. playerPosition = Mathf.Clamp(playerPosition, leftBoundary, rightBoundary);
    4. player.transform.position.x = playerPosition;
    5.  
    Instead of hard-coding things like "20 * Time.deltaTime", I always try to make stuff like that into variables that are exposed to the inspector, which makes tweaking and code reusability a lot simpler.

    --Eric
     
  14. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Javascript vs C#, I am using C#, so I can't cut corners like your both suggesting. There is strict rules I have to abide by, ran into issues, so yup, I need Vector3 with C#, thats discussed in another thread where I was having problems. This wraps back to javascript is less strict on coding than C# is.

    I agree with the dynamic for speed, right now I am going for functional, as I get things to work, I adjust for dynamic. I have to hard code the left and right boundry since screen.width and screen.height return values outside the viewable screen, just to name a few issues I am running into. Its all good though, learning experiences.

    For C#, I physically can't use player.transform.position.x, I literally have to take the Vector3[0] element for X , Vector3[1] element for y and Vector3[2] element for Z, update each directly, then apply the adjusted vector back to player.transform.position

    Its back to the C# vs Javascript thing.
     
  15. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    I use C#. You can use .x. You just can't do it inline with the transform member, as I pointed out in the other thread (transform.position.x = 10). You can certainly do someVector3.x = 10 on its own line, though.

    And you can do what Eric is suggesting, too. You seem to have completely missed the concept of editing variables via the inspector...
     
  16. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    I am going to have to come back to the inspector thing after I get done with this project and study it more then optimize this project. Something is simply not clicking.
     
  17. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    It will be worth the time to get it to click. It really speeds up tweaking to get the "right" feel.

    All Eric was saying was to declare a public variable in your script that you can then adjust in the Inspector and use that variable in your calculations. Trying to remember C#
    Code (csharp):
    1.  
    2. public class YourClass : MonoBehavior
    3.  
    4.   public int SpeedFactor = 20;
    5.  
    6. ... some of your code ...
    7.  
    8.   if (Input.GetKey("a"))
    9.     {
    10.       pPos=activePlayer.transform.position;
    11.       pPos[0] -= SpeedFactor * Time.deltaTime; // instead of 20 * Time.deltaTime
    12. ....
    13.  
    In this case SpeedFactor will be exposed in the inpector. If you change the value from 20 in the inspector, that value is used instead of 20. The 20 in your script is the "default" value if the value is never changed in the Inspector.
     
  18. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    I think the disconnect is the assumption I have my game objects in the scene, I don't. This is my inspector display and its pointing to the code that does everything. Even when I choose my ScriptManager cube object, there is nothing in the inspector to utilize.

    The start of my code branch is as follows:
    Code (csharp):
    1.  
    2. public class SetupObjects : MonoBehaviour {
    3.     GameObject clone;
    4.     Vector3 pos;
    5.     Vector3 modScale;
    6.     string[] invList=new string[5];
    7.     string tmpType;
    8.     int Lives=3;
    9.     enum LifeStatus{Alive=0, Dead=1, Starting=2};
    10.     LifeStatus currentStatus;
    11.     GameObject activePlayer;
    12.    
    13.     GameObject objSaucer;
    14.     GameObject[] objHighInvader;
    15.     GameObject objMidInvader;
    16.     GameObject objLowInvader;
    17.    
    18.     Vector3 pPos;
    19.     float fieldLeft=-120;
    20.     float fieldRight=120;
    21.  
    As you can see, they do not show up in the inspector, thus why nothing is clicking...

    When I run my game you will see (clone) next to all objects, this is because I am creating everything in code. Literally.
     

    Attached Files:

  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    In C#, you need to declare your variables as public before they show up in the inspector. You don't have to have anything in the scene to utilize this.

    Code (csharp):
    1.  
    2. public class SetupObjects : MonoBehaviour {
    3.    public GameObject clone;
    4.    // etc.
    5.  
    --Eric
     
  20. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    That explains alot :)
    (no pun intended) so everything not declared public is by default private to the script and not public to the game. Works for me. Going to play with that some.

    Edit..
    HAH thats cool.. ok back to my chaos...
     
  21. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yeah, in Javascript it's the other way around:

    Code (csharp):
    1.  
    2. var whatever : Something;
    3.  
    is exposed by default (unless it's inside a function), so you have to do

    Code (csharp):
    1.  
    2. private var whatever : Something;
    3.  
    in order to hide it.

    --Eric
     
  22. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    So if I declare my game objects public, then I can drag prefabs on them, then do instansiate somehow on those instead of load resource right?
     
  23. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Yup. There is some dark magic with Instantiate() that isn't super clear, too. Let me try to elucidate:

    If you have public RigidBody missile in your script, the inspector will add a slot to edit it. You can drag any GameObject with a RigidBody component--prefab or scene--to that slot. You can then use that member variable as you would expect: missile.AddForce(), etc.

    You can also call Instantiate() and pass it the missile variable. This is where Unity does a few things for you. Unity will duplicate the GameObject the RigidBody component was on and return the new RigidBody component.

    So if you have a prefab with your user-made LogicAI component on it, you can have a public LogicAI someVariable member variable, drag the prefab to it in the inspector, and then pass it to Instantiate(). Unity will create the entire prefab in your scene and return the new LogicAI component for you to interact with.

    So just keep in mind that you don't need to Instantiate() just GameObjects. In fact, it's easier in Unity to deal with only the object types you care about. You usually don't expose GameObjects--rather, you expose the component type you care about (RigidBody, GUIText, etc). There usually isn't much need to deal with generic GOs.
     
  24. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Its alot to soak in, I have dragged a prefab on a public game object, but haven't gotten the instansiate to work yet without the resource load, I'll keep plugging at it, many thanks for all your help.

    Edit:
    Is there a maximum number of public objects?
     
  25. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Wow thats cake once you understand it!
    Guess you might be proud of me.. *pats self on back*
    Thanks to all this information, I got it figured out!
    Check out this code now that I got rid of resource load (reminds me, must move all stuff out of resource folder now and into normal folders)
     

    Attached Files:

  26. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    One slight "gotcha" in that area, I've found, is that if you have a public variable typed as a rigidbody (for example) and later decide you need to change it in your script to a gameobject or something else for whatever reason, it stays as a rigidbody in the inspector and you get casting errors. Even though your script is correct. So you have to remove it in the inspector and then re-add it before it will be the correct type.

    --Eric
     
  27. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Thanks for that tip.
    Note to self, always clean inspector if changing object types for public items.