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

RequireComponent for a GameObject Variable

Discussion in 'Scripting' started by nsvir, May 11, 2014.

  1. nsvir

    nsvir

    Joined:
    Apr 29, 2014
    Posts:
    3
    I am doing this verification for a gameobject

    Code (csharp):
    1. public class ElementManager : MonoBehaviour {
    2.  
    3.     public GameObject prefab;
    4.  
    5.     private void Start() {
    6.         if (this.prefab == null || this.prefab.GetComponent<MyScript>() == null)
    7.                      this.enabled = false
    8.     }
    9. }
    Can I use something like RequireComponent but on a public variable ?

    Getting a compilation error and allow me to not check those conditions all the time ?

    Thanks in advance :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    i think you'll have to nest those two... checking the prefab is null, then checking if the prefab has the component it needs (which will error if the prefab is null).

    psuedo
    Code (csharp):
    1.  
    2. if(prefab==null)
    3. {
    4. if(prefab.component == null)
    5. {
    6. }
    7. }
    8.  
     
  3. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Nesting isn't needed because the second operand will never be evaluated if the first operand is true. It's called "short-circuit evaluation." The opposite applies to the operator, i.e. if the first operand is false, then the second operand will not be evaluated.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    You can enforce your type as variable instead of GameObject.

    Code (csharp):
    1. public class ElementManager : MonoBehaviour
    2. {
    3.     public MyScript prefab;
    4.  
    5.     private void Start()
    6.     {
    7.         if (this.prefab == null)
    8.             this.enabled = false
    9.     }
    10. }
    Is that what you need?
     
  5. nsvir

    nsvir

    Joined:
    Apr 29, 2014
    Posts:
    3
    Thanks for your answer.
    This is better but I do need the GameObject because I want to instantiate it.
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Code (csharp):
    1.  
    2. prefab.gameObject;
    3.  
     
  7. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    You should still be able to instantiate via a component reference:
    Code (csharp):
    1.  
    2. public class ElementManager : MonoBehaviour
    3. {
    4.     public MyScript prefab;
    5.  
    6.     private void Start()
    7.     {
    8.         if (prefab == null)
    9.             enabled = false;
    10.     }
    11.  
    12.     private void Foo() {
    13.         MyScript prefabInstance = Instantiate(prefab) as MyScript;
    14.     }
    15. }
    16.  
     
  8. nsvir

    nsvir

    Joined:
    Apr 29, 2014
    Posts:
    3
    Wow! This is very powerfull.

    I didn't think I could give a prefab that would be hold by his script in a public variable and keep the prefab components.

    I don't know how UnityScripts can do that ^^

    Thanks for your help anyway!

    It's a great community :)