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. The 2022.2 beta is now available for testing. To find out what's new, have a look at our 2022.2 feature highlights.
    Dismiss Notice
Dismiss Notice
Submit bug reports tagged with #Beta2022Win_Unity when you encounter unknown issues while testing the 2022.2 betas for a chance to win a year of Unity Pro. For more information, have a look at our Beta Sweepstakes Announcement.

please clarify: Using GameObject.AddComponent is no longer allowed and will throw an exception

Discussion in '5.4 Beta' started by freekstorm, Jun 20, 2016.

  1. freekstorm

    freekstorm

    Joined:
    Nov 11, 2010
    Posts:
    86
    beta 22 release notes:

    Does this mean that AddComponent<RigidBody>() will now break?
     
    Last edited: Jun 20, 2016
  2. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,025
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class test : MonoBehaviour {
    5.  
    6.     IEnumerator Start () {
    7.         yield return new WaitForSeconds(4.0f);
    8.         this.gameObject.AddComponent<Rigidbody>();
    9.     }
    10.  
    11. }
    12.  
    13.  
    Works for me using B21.
     
  3. freekstorm

    freekstorm

    Joined:
    Nov 11, 2010
    Posts:
    86
    The OP is taken from the beta 22 release notes. Not beta 21!
     
  4. TuQueNinja

    TuQueNinja

    Joined:
    Nov 4, 2015
    Posts:
    1
    Scary, very scary stuff. We would like some clarification on this as well please.
     
  5. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    595
    It seems to imply you could previous call AddComponent with a non-monobehaviour class and it'd work automagically? If so, that was some sneaky dirty behaviour =)
     
  6. Glenn-Anderson

    Glenn-Anderson

    Joined:
    Sep 17, 2014
    Posts:
    140
    If only MonoBehaviours were intended to be added with "AddComponent", 1) Why would it be called "AddComponent"? and 2) Why would it accept any Component class rather than only MonoBehaviour classes?

    Also, I made a test project with b22. AddComponent<Rigidbody>() and AddComponent(typeof(Rigidbody)) do not throw any exceptions... EDIT: See below.
     
    Last edited: Jun 20, 2016
    tuQueTrip likes this.
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,643
    /facepalm. Sorry about this guys. Looks like HTML formatting issue. The original release note reads as follows:

    You can still use AddComponent to add any of the conventional components, but you can no longer add untyped MonoBehaviours.
     
    Zuntatos likes this.
  8. Alex-Lian

    Alex-Lian

    Guest

    and fixed in the release notes....ah, html escaping. bah.
     
  9. freekstorm

    freekstorm

    Joined:
    Nov 11, 2010
    Posts:
    86
  10. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    @Tautvydas Zilys Can you explain why do that ?

    It's a big problem for me, i have a class like this in my project :

    Code (CSharp):
    1. public abstract class ManagerBehaviour<T> : MonoBehaviour where T:MonoBehaviour
    2. {
    3.     protected internal static T m_instance;
    4.  
    5.     public static T GetOrCreateInstance()
    6.     {
    7.         if (m_instance == null)
    8.         {
    9.             m_instance = new GameObject(typeof(T).Name).AddComponent<T>();
    10.         }
    11.         return m_instance;
    12.     }
    13. }
    So i can't do that anymore ?

    Edit : I tested this code and it's ok, but i don't understand what have change so ?
    I have an AddComponent exception with this code :
    Code (CSharp):
    1. CrashReporting.Init( "XXX", "XXX", "XXX" );
     
    Last edited: Jun 21, 2016
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,643
    You can still do it as long as T is something derived from MonoBehaviour, rather than MonoBehaviour itself.
     
  12. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Ok thank you
     
  13. JonTuque

    JonTuque

    Joined:
    Sep 16, 2014
    Posts:
    3
    We are also getting the "AddComponent with Monobehaviour is not allowed." error with the following call:
    UnityEngine.CrashLog.CrashReporting.Init("XXX", "XXX", "XXX");

    I'm assuming you'll fix that, but in the meantime, will CrashReporting still work as before?
     
  14. Alex-Lian

    Alex-Lian

    Guest

unityunity