Am scripting using C# in Unity but cant run code - c#

I'm getting started with scripting in Unity and after running the attached codes. i get the error in the unity console. Any assistance for me.
Error in unity image.
This the the code i run in the VS for the unity.
enter code here
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start method is Called");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update is calling");
}
}**

you have 2 Classes named CubeScript in your project. its the reason of error
Look at other script if one contains the same name of class
to avoid duplicate classes, you could add namespace too:
namespace NameOfSceneForExample
{
public class CubeScript : MonoBehaviour
{
}
}

Related

Loading a script externally

How do I load a script externally? As in from streaming assets? I would like to make changes to the code. For example a simple code like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetValue: MonoBehaviour
{
private int value = 12;
// Start is called before the first frame update
void Start()
{
}
}
Drag the .cs file into the project.

Assets/Scripts/Quıt.cs(10,21): error CS0117: 'Application' does not contain a definition for 'Quıt'

I started playing games for the first time in Unity. I get the error in the title. Why could it be? Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;
public class Quıt : MonoBehaviour
{
{
Application.Quıt();
}
}
It looks like you just need to put it in a method. Also Application.Quit() does not work in the editor.
public class Quit : MonoBehaviour {
public void QuitApplication() {
Application.Quit();
}
}
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods
https://docs.unity3d.com/Manual/MobileOptimizationScriptingMethods.html

Error: "NullReferenceException: Object reference not set to an instance of an object" for 2 scripts on one object in Unity

There is a chance this is a duplicate, but I have tried and tried to get this to work and it's probably something simple.
I'm trying to get two scripts to interact on one object. I would like a basic generic physics script that will handle all the interactions for all my objects.
Script 1:
// Simple Player1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player1 : MonoBehaviour
{
Physics physics_script;
void Start(){
physics_script = gameObject.GetComponent<Physics>();
}
// Update is called once per frame
void Update() {
physics_script.helloWorldFromAnotherScript();
}
}
Script 2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Physics : MonoBehaviour
{
void Start(){
}
void Update() {
}
public void helloWorldFromAnotherScript()
{
Debug.Log("Hello world from player");
}
}
Edit:
I'm pretty sure the problem is here:
But I can't change it to save my life.
It should work.
Are you sure you have BOTH scripts added to the gameObject? At the image I only see player1, and I doubt if Physics it's added.
Elsewhere you can make physics_scripts public or serialize them(with [SerializeField]) and you will be able to drag'n'drop at the editor the script to the formfield. Thats allows you to not use the GetComponent<> at start.
Remember to delete start and update methods if you dont use them.

Access public static variable from another script Unity

I am trying to access and change of the value of a variable from another script, I have watched various different tutorial on youtube but cannot seems to do it. It always come up with an error as following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GlobalValues : MonoBehaviour
{
public static bool c2u;
}
And the other script
public class Choosing : MonoBehaviour {
private void Start()
{
GlobalValues.c2u = false;
}
}
In the "Choosing" script, the GlobalValues always shows "'GlobalValues' does not exist in this current context". What can i do to fix this problem
That code should work fine. The only way I could see it not working is if you haven't saved one of the scripts.

Unity3d no Monobehaviour scripts in the file, or their names don't match the file name

Hello everyone I've been working on my first game and suddenly I got this error and cannot run the game, I already installed the new version of unity but it persists. I see it can be caused for several reasons but had no luck so far, do you know the most probable causes and how to fix it?
When I select the scripts the only one where I do not get this error is the following:
using UnityEngine;
using Assets.Code.States;
using Assets.Code.Interfaces;
public class StateManager : MonoBehaviour
{
private IStateBase activeState;
void Start ()
{
activeState = new BeginState (this);
}
void Update ()
{
if (activeState != null)
activeState.StateUpdate();
}
void OnGUI()
{
if (activeState != null)
activeState.ShowIt ();
}
public void SwitchState(IStateBase newState)
{
activeState = newState;
}
}
But for example here I get the error:
using UnityEngine;
using Assets.Code.Interfaces;
namespace Assets.Code.States
{
public class BeginState : IStateBase
{
private StateManager manager;
public BeginState (StateManager managerRef)
{
manager = managerRef;
Debug.Log ("Constructing BeginState");
Time.timeScale = 0;
}
public void StateUpdate()
{
if (Input.GetKeyUp(KeyCode.Space))
manager.SwitchState(new PlayState (manager));
}
public void ShowIt()
{
if (GUI.Button (new Rect (10, 10, 150, 100), "Press to Play"))
{
Time.timeScale = 1;
manager.SwitchState (new PlayState (manager));
}
}
}
}
And so on with every other script.
I've already installed a newer version of unity3d, uninstalled the antivirus, checked the file names but the error persists. I also do not have any class in a namespace..
Make sure your script file is named exactly as the MonoBehaviour class it contains.
MyScript.cs:
using UnityEngine;
public class MyScript : MonoBehaviour
{
}
Also, if your script file contains helper classes, make sure they are placed on the bottom of the file, never on top of the MonoBehaviour subclass.
Also, Unity will sometimes have problems when your MonoBehaviour classes are in namespaces. I don't know when exactly this happens, it simply does every now and then. Taking the class out of a namespace fixes the error.
I had a strange variation of this issue, and did not see the solution posted anywhere else online.
TLDR
I had whitespace in the namespace for the classes which Unity could not identify as MonoBehaviours.
Background
I had the No MonoBehaviour scripts in the file or ... warning text on two script files in my project, one a MonoBehaviour, and the other a ScriptableObject. Each issue had different symptoms:
MonoBehaviour
The MonoBehaviour class was named like MyMonoBehaviourService.
The script file was named MyMonoBehaviourService.cs
The class was not partial, it had no nested types, nor any other types defined within the file.
I could not attach MyMonoBehaviourService to a GameObject via the inspector, but I could via gameObject.AddComopnents<MyMonoBehaviourService>(). This resulted in the script attached to the object, looking and behaving correctly.
Upon entering PlayMode, I would get the error: The referenced script (Unknown) on this Behaviour is missing!
After exiting PlayMode, the GameObject with the MyMonoBehaviourService attached would now say that it was missing.
Scriptable Object
The ScriptableObject class was named like MyScriptableServiceData.
The script file was named MyScriptableServiceData.cs.
The class was not partial, it had no nested types, nor any other types defined within the file.
I was able to create a MyScriptableServiceData via C# and save it using the AssetDatabase.
I was then able to see and modify the properties using the inspector.
Upon exiting and re-opening the project, the asset would say that its script was missing.
Resolution
Strangely enough, the issue was actually whitespace in the namespace for each class. I mistakenly thought I was seeing word-wrapping but actually, there was a newline in the namespace.
namespace Appalachia.Prototype.KOC.Areas.DeveloperInterface.V01.Features.PerformanceProfiling.Services.
FPS
{
public sealed class FPSProfilerService : MonoBehaviour
{
}
}
needed to be changed to this:
namespace Appalachia.Prototype.KOC.Areas.DeveloperInterface.V01.Features.PerformanceProfiling.Services.FPS
{
public sealed class FPSProfilerService : MonoBehaviour
{
}
}
I'm not sure why this causes an issue with MonoBehaviour/ScriptableObject detection but it really took a while for me to uncover.

Categories

Resources