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

Add Component Crashes Unity?

Discussion in 'Scripting' started by Faestus, Jul 25, 2014.

  1. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    This is the code.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. class TileAttributes
    4. {
    5.     var Type:int=0;
    6.     var X:int=0;
    7.     var Y:int=0;
    8.     var Z:int=0;          
    9. }
    However, for some reason, this causes a crash!
    Code (javascripit):
    1.  
    2. function DrawTile(X:Number,Y:Number,Z:Number,TYPE:Number)
    3. {
    4.     var TILE : GameObject  = GameObject.CreatePrimitive(PrimitiveType.Cube);
    5.     TILE.transform.position = Vector3(X*game.TileSize, Y*game.TileSize, Z*game.TileSize*2);
    6.     TILE.transform.localScale = Vector3(game.TileSize,game.TileSize,game.TileHeight);  
    7.     TILE.AddComponent(TileAttributes);
    8.     return TILE.GetComponent(TileAttributes);
    9. }
    Can someone help me?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    TileAttributes needs to extend MonoBehaviour. Also - AddComponent will return the component to you so your subsequent GetComponent call is unnecessary.
     
  3. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Well the 2nd code is in Javascript and includes #pragma strict in the beginning. Is there anything else I need to add?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Yes, you need have the class extend MonoBehaviour. The easiest way to do that is to get rid of the class definition entirely, since scripts in Unityscript are automatically classes that extend MonoBehaviour anyway.

    --Eric
     
  5. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    I need to refference the class and add it dynamically to objects.

    Shall I make it a function?

    EDIT:Nvm, got what you mean, Eric.

    Ty for the help guys!
     
    Last edited: Jul 25, 2014