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.
Related
i'm currently making a quiz game using unity with guidance from youtube( this is the video that i follow https://www.youtube.com/watch?v=G9QDFB2RQGA). now i'm stuck at the when i tried to add the answer script into buttons component (14:30). everytime i tried, the error message the script don't inherit native class pop out. have been a day trying to find solution and i still can't find it. tried to change the script file name, class name to be identical, etc, but still cant add the script. there's no compiler error tho. anyone know whats wrong?
Answers.cs
using System.Collections.Generic;
using UnityEngine;
public class Answers : MonoBehaviour
{
public bool isCorrect = false;
public Quizmanager quizmanager;
public void Answers()
{
if (isCorrect)
{
Debug.log("Correct answer!");
else
{
Debug.log("Wrong answer ~");
}
}
}
Quizmanager.cs
using System.Collections.Generic;
using UnityEngine;
public class Quizmanager : MonoBehaviour
{
public List<Questions> QnA;
public GameObject[] options;
public int currentquestions;
public Text Questiontxt;
private void Start()
{
generatequestion();
}
public void correct()
{
QnA.RemoveAt[currentquestions];
generatequestion();
}
void Setanswers()
{
for(inr 1=0; 1 <options.Length; int++)
{
options[i].GetComponent<AnswersScript>().isCorrect = false;
options[i].transform.GetChild(0).GetComponent<Text>().text = QnA[currentquestions].Answers[i];
if(QnA[currentquestions].CorrectAnswer == i + 1)
{
options[i].GetComponent<AnswerScript>().isCorrect = true;
}
}
}
void generatequestion()
{
currentquestions = Random.Range(0, QnA.Count);
Questiontxt.text = QnA[currentquestions].Question;
Setanswers();
}
}
I've had similar issues while using visual studio. The only way I managed to solve them was by creating a new script, and copying the class content (not the class name) into the new file and deleting the old file. The class name might change and the solution is a bit anticlimactic but it usually saves me from this issue.
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.
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
{
}
}
I am trying call a function from a script on another game object to play an animation clip, but it gives me the following error:
The type 'TitleAnimScript' in 'c:\Users\ruanv\Documents\Unity
Projects\Project_MathNinja\Assets\Scripts\TitleAnimScript.cs'
conflicts with the imported type 'TitleAnimScript' in
'Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null'. Using the type defined in
'c:\Users\ruanv\Documents\Unity
Projects\Project_MathNinja\Assets\Scripts\TitleAnimScript.cs'.
[Assembly-CSharp]
The error is in this 'SceneManagerScript.cs' script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class SceneManagerScript : MonoBehaviour
{
[SerializeField]
private GameObject mainMenuButtons;
[SerializeField]
private GameObject levelSelectButtons;
private TitleAnimScript titleAnimScript;
void Start()
{
// adsf
}
public void QuitApp()
{
Application.Quit();
}
public void LevelSelect()
{
mainMenuButtons.SetActive(false);
titleAnimScript.playAnimClip();
levelSelectButtons.SetActive(true);
}
}
And this is the 'TitleAnimScript.cs' script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class TitleAnimScript : MonoBehaviour
{
private Animator titleTextAnim;
void Start()
{
titleTextAnim = GetComponent<Animator>();
}
public void playAnimClip()
{
titleTextAnim.SetTrigger("playClip");
}
}
It sounds like you've made a copy of your TitleAnimScript in a reserved folder. These reserved folder will compile to a sperate DLL called, as you could guess, Assembly-CSharp-firstpass. They're created as two separate projects, which is why the IDE won't pick it up as an error, but because you're referencing Assembly-CSharp AND Assembly-CSharp-firstpass, the solution is seeing two versions of TitleAnimScript.
Check your reserved folders, as described on this page, for any second occurrences of your TitleAnimScript. It could be that you just accidently copied it across at some stage.
For a quick refernce, the folders are:
Editor
Editor Default Resources
Gizmos
Plugins
Resources
Standard Assets
StreamingAssets
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.