The Name 'GUILayout' does not exist in the current context - unity3d

I'm doing procedural terrain generation in unity from Sebastian Lague's tutorial, and this code isn't working. How can I fix that?
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[CustomEditor (typeof(MapGenerator))]
public class MapGeneratorEditor : Editor {
public override void OnInspectorGUI() {
MapGenerator mapGen = (MapGenerator)target;
DrawDefaultInspector ();
if (GUILayout.Button ("Generate")) {
mapGen.GenerateMap ();
}
}
}

You should also include UnityEngine namespace
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

Related

Why is my unity scene script not working?

using a button to load a scene and iam just getting the error "Assets\Scripts\button_press.cs(15,59): error CS1503: Argument 1: cannot convert from 'UnityEngine.Object' to 'string'"
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class button_press : MonoBehaviour
{
public Object sceneToLoad;
public void PlayGame ()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad);
}
public void QuitGame()
{
Debug.Log (" You have quit");
Application.Quit();
}
}
The LoadScene API expects either an integer referring to the scene number in the build, or a string referring to the name of the scene (generally preferred).
You should define your sceneToLoad field as a string rather than an Object.

Unity Text(Legacy) disapears on WebGL build

I am trying to make a FlappyBird on WebGL with Unity. I made scores system, and there is no bug finded while runing on unity editor, but when I build with WebGL, Text don't shows up. Only legacy Text (Not TextMeshPro) causes this problm, so it would be helpful too if there is a way to use TextMeshPro. https://r0k0r.github.io/FlappyBirdWebGL is my game link, and https://github.com/R0K0R/FlappyBirdWebGL is my github link. I don't know this will help, but I am currently coding with ubuntu.
this is my Score.cs code that returns current score:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public static int score = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(){
score++;
}
public static int returnScore(){return score;}
}
and this is my ApplyScore.cs code that applys score to text gameobject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ApplyScore : MonoBehaviour
{
public Text ScoreText;
// Start is called before the first frame update
void Start()
{
ScoreText.text = "0";
}
// Update is called once per frame
void Update()
{
ScoreText.text = Score.returnScore().ToString();
}
}
this is what it looks like
and this is what it should look like

Unity URP 2D light

I've recently discovered the URP and am developing a 2D game for fun. I added a global 2d light and set up some code to grab the light component, but unity keeps saying that light 2D does not exist. The error in unity is "error CS0246: The type or namespace name 'Light2D' could not be found". This is the code I have so far. I also followed a tutorial by Jimmy Vegas to grab the system time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class DayNight : MonoBehaviour
{
public GameObject theDisplay;
public GameObject GL;
public int hour;
public int minutes;
public int totaltime;
private bool AM;
void Start()
{
totaltime = System.DateTime.Now.Hour;
hour = System.DateTime.Now.Hour;
minutes = System.DateTime.Now.Minute;
}
public void Night()
{
//change light settings
GL.GetComponent<Light2D>().Intensity = 0.4;
}
}
To reference the Light2D Component you need to add the UnityEngine.Experimental.Rendering.Universal namespace to your code.
Namespace Example:
...
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
public class DayNight : MonoBehaviour
{
...
}
If you get an Error you need to just continue and try to compile it, to see if it actually works.
After that is done you should be able to access the Light2D Component of your GameObject without any further troubles.
Calling Light2D Example:
public float nightvalue = 0.4f;
public void Night()
{
// Change light intensity to nightvalue.
GL.GetComponent<Light2D>().intensity = nightvalue;
}

NullReferenceException using ScriptableObjects in Unity

I have setup a Scriptableobject in database called "Card" and am attempting to make a searchable list of those items in the scriptableobject. I attached CardDatabase script to an empty gameobject called _CardDatabase. I am not sure if I am attaching it incorrectly or what I have done wrong here.
I am still getting the following error:
NullReferenceException: Object reference not set to an instance of an object
CardDatabase.GetCardByID (System.String abbr) (at Assets/Scripts/CardDatabase.cs:33)
// CardDatabase.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class CardDatabase : MonoBehaviour
{
public CardList cards;
private static CardDatabase instance;
private void Awake()
{
if (instance = null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public static Card GetCardByID(string abbr)
{
return instance.cards.AllCards.FirstOrDefault(i=> i.abbr == abbr);
}
}
// CardList.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Cards Database",menuName = "Cards Database")]
public class CardList : ScriptableObject
{
public List<Card> AllCards;
}

Unity Photon Sync Coins for all Players

I have an problem on Synchronizing an Integer.
When I collect with the first Player (The First Connected with the room) a Coin Than the Text(UI) of the Coins are Changing on all Clients but if I collect with an other Player it Only Change for hisself.
How Can I fix this Problem?
This is the Code for Synchronizeing the Coins:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoneyManager : MonoBehaviour{
public int Coins;
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if(stream.isWriting){
stream.SendNext(Coins);
}else if(stream.isReading){
Coins=(int) stream.ReceiveNext();
}
}
}
This is the Code for changing The Text of the UI Element
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UiManager : MonoBehaviour{
public Text Coins;
void FixedUpdate(){
Coins.text=" Coins: "+GameObject.Find("MoneyManager").GetComponent<MoneyManager>().Coins;
//some Other Code for other Things...
}
}
If you need some other things to know just ask.