Search Unity

Unity c# You are trying to create a MonoBehaviour using the 'new' keyword error

Discussion in 'Scripting' started by Deleted User, Nov 28, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hey guys so I got this annoying error, I don't get why in Unity we can't access other classes like this. I can't even inherit because you can't have 2 base classes. My other script has a variable which I want to reset the number after the battery is collected but it aint working.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BatteryPickup : MonoBehaviour {
    5.  
    6.    
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.        
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         RaycastHit detectBatteries;
    18.         if (Physics.Raycast(transform.position, transform.forward, out detectBatteries, 4))
    19.         {
    20.             if (detectBatteries.collider.tag == "Batteries" && Input.GetKeyDown(KeyCode.Q))
    21.             {
    22.                 Debug.Log("hi");
    23.                 Destroy(detectBatteries.collider.gameObject);
    24. //here is fails
    25.                 flashlight ewa = new flashlight();
    26.                 ewa.flashlightTimer = 55;
    27.             }
    28.         }
    29.     }
    30. }
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    use gameObject.AddComponent<flashlight>();

    Or make the flashlight not inherit from MonoBehaviour.

    You're not allowed to call new because Unity wants to be sure it has setup MonoBehaviours correctly using AddComponent so you don't get null gameObject variables etc..
     
  3. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    501
    What Antony said. Also, you can have an inheritance chain of MonoBehaviours just fine.

    One more things: you are currently raycasting every frame, but are only doing something with it if the user is pressing Q - this is very wasteful. You should check if the user is pressing Q first, and only then do the raycast.
     
  4. Deleted User

    Deleted User

    Guest

    Oh I see thanks for feedback, I have been told optimization is for later, but I will do it thanks. Should I just copy and paste that code into start function?
     
  5. Deleted User

    Deleted User

    Guest

    I did that but how do I access the variable and reset it to 55?
     
  6. Deleted User

    Deleted User

    Guest

    help please
     
  7. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Code (csharp):
    1.  
    2. flashlight ewa;
    3.  
    4. void Start()
    5. {
    6.      ewa = gameObject.AddComponent<flashlight>();
    7. }
    8.  
    9. void Update()
    10. {
    11.      // the rest of your code goes here
    12.      {
    13.           ewa.flashlightTimer = 55;
    14.      }
    15. }
    16.  
    I think this is wrong though, as you'd be adding a flashlight to a gameObject that is a battery pack. is this what you want to be doing? Wouldn't you already have a flashlight somewhere and you'd be adding the battery to that? In which case you'd want to get the flashlight from somewhere else rather than adding a new one.
     
    Last edited: Nov 28, 2015
  8. Deleted User

    Deleted User

    Guest

    Doesn't work, when I type AddComponent it doesn't pop up.
     
  9. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778

    Sorry,

    ewa = gameObject.AddComponent<flashlight>();

    p.s. I edited my previous post to correct that.
     
  10. Deleted User

    Deleted User

    Guest

    Works! But when I start the game I get this error

    MissingComponentException: There is no 'Light' attached to the "First Person Controller" game object, but a script is trying to access it.
    You probably need to add a Light to the game object "First Person Controller". Or your script needs to check if the component is attached before using it.
    flashlight.Update () (at Assets/scripts/flashlight.cs:33)

    Do you want the code from my flashlight script?
     
  11. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Not really.

    I think you need to read through the Unity manual and do some tutorials to get a better understanding of how Unity works.
     
    Thomas-Mountainborn likes this.