Search Unity

Adding component with custom properties

Discussion in 'Scripting' started by chippy-cheese, Apr 14, 2012.

  1. chippy-cheese

    chippy-cheese

    Joined:
    Feb 29, 2012
    Posts:
    124
    I have a script with basic stuff nothing more than bools. They are all false to start out with too. I want to make one of them true before adding on a script to different object. Any ideas i have tried many ways to do so but nothing has worked so far.

    Example i want to have bleeding turned on but not any others.


    This is what I have so far.

    using UnityEngine;
    using System.Collections;

    public class test_script3 : MonoBehaviour {
    void Start () {
    }
    void Update () {
    if(Input.GetKeyDown(KeyCode.Alpha3)){
    this.gameObject.AddComponent("condition");
    }
    }
    }

    using UnityEngine;
    using System.Collections;

    public class condition : MonoBehaviour {

    public bool dazed;
    public bool blinded;
    public bool bleeding

    };
     
  2. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Either initialize it on declaration

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. // Classes and Methods in C# are usually upper case to follow the general naming guidelines: [B]Condition[/B] not [B]condition[/B]
    6. public class condition : MonoBehaviour {
    7.  
    8.     public bool dazed = true; // initialize it, the default value for bool is false
    9.     public bool blinded;
    10.     public bool bleeding
    11.  
    12. };
    13.  

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class test_script3 : MonoBehaviour {
    6.     void Start () {
    7.     }
    8.     void Update () {
    9.         if(Input.GetKeyDown(KeyCode.Alpha3)){
    10.             condition c = this.gameObject.AddComponent<condition>();
    11.             c.blinded = true;
    12.         }
    13.     }
    14. }
    15.