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

How can I create a object???

Discussion in 'Scripting' started by paulorsg, May 30, 2017.

  1. paulorsg

    paulorsg

    Joined:
    Oct 25, 2016
    Posts:
    8
    Hello I can't creat a simple object from one of my classes, unity keep sending me a warning message like this

    You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
    UnityEngine.MonoBehaviour:.ctor()

    and I know I can't creat an object that inherit from a monoBehavior using the word new, but how can I do that ???
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Post some code using code tags please and what is it you are trying to do? Why are you creating a the class?
     
  3. paulorsg

    paulorsg

    Joined:
    Oct 25, 2016
    Posts:
    8
    I'm trying to do this :
    Player_Movement_Controller_Class playerMovementControllerObject = new Player_Movement_Controller_Class();

    The class inherit from MonoBehavior
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    It's telling you exactly what to do. You use AddComponent to add a script to an existing gameobject.
     
  5. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Code (csharp):
    1.  
    2. Class A : Monobehaviour
    3. {
    4. public int x = 10;
    5. ///some code.
    6. }
    7.  

    Instead of:
    Code (csharp):
    1. ClassA a = new ClassA();
    Put Monobehaviour class on a gameObject and use
    Code (csharp):
    1. ClassA a = gameObject.GetComponent<ClassA>();
    OR
    to attach a component on GameObject use
    Code (csharp):
    1. ClassA a = gameObject.AddComponent<ClassA>();
     
  6. paulorsg

    paulorsg

    Joined:
    Oct 25, 2016
    Posts:
    8
    But now I can't use my object !!!
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    Then you're going to have to provide more info on what you are doing because we aren't going to have a clue how to help you with what info you provided.
     
  8. paulorsg

    paulorsg

    Joined:
    Oct 25, 2016
    Posts:
    8
    I have Instantiated the object like he said, but I can't use the methods outside of the start function

    void Start()
    {
    Player_Movement_Controller_Class playerMovementControllerObject = gameObject.GetComponent<Player_Movement_Controller_Class>();
    }
     
  9. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Did you declare your methods in that class as public?
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    That's because you are declaring your variable as local and not global.

    You should be doing this

    Code (CSharp):
    1. Player_Movement_Controller_Class playerMovementControllerObject;
    2.  
    3. void Start()
    4. {
    5. playerMovementControllerObject = gameObject.GetComponent<Player_Movement_Controller_Class>();
    6. }
    7.  
    8. void SomeOtherMethod()
    9. {
    10. playerMovementControllerObject .DoSomethingMethod();
    11. }
    And as @cstooch mentioned, public methods if they aren't already.
     
    Ryiah and cstooch like this.
  11. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Jeez, my reading comprehension has been crap these days. Haha I misread the post. While public methods would be needed, the issue is definitely related to what you pointed out.. scope of the component instance he created.

    I initially took the post as meaning that he couldn't access any methods in the class except Start (which really makes no sense he'd be calling that, but I still didn't quite catch on :p).
     
  12. paulorsg

    paulorsg

    Joined:
    Oct 25, 2016
    Posts:
    8
    I just want to thanks everyone for the help.