I'm trying to build a Unity game, and keep getting the error:
Assets\charaterselection.cs(34,9): error CS0103: The name 'PrefabUtility' does not exist in the current context
The issue is I imported UnityEditor, I'm not sure what's going on
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
public class charaterselection : MonoBehaviour
{
public SpriteRenderer sr;
public List<Sprite> skins = new List<Sprite>();
private int selecectedSkin;
public GameObject player;
public void Next()
{
selecectedSkin=selecectedSkin+1;
if (selecectedSkin== skins.Count)
{
selecectedSkin=0;
}
sr.sprite= skins[selecectedSkin];
}
public void back()
{
selecectedSkin = selecectedSkin - 1;
if (selecectedSkin < 0)
{
selecectedSkin = skins.Count - 1;
}
sr.sprite = skins[selecectedSkin];
}
public void play()
{
PrefabUtility.SaveAsPrefabAsset(player, "Assets/Players/FROGY.prefab");
SceneManager.LoadScene(1);
}
}
Thank you guys for your help, I literately just made a file called "Editor" and it worked.
Related
I am trying to make a simple inventory in Unity 3D for virtual environment. I am using StemVR plugin, because I have HTC Vive. I was following this tutorial, made for Oculus (https://www.youtube.com/watch?v=gAz_SeDUQBk&t=310s) and now I have to adapt the code for HTC Vive, but I don't know how.
In the slot script I have a problem with this part of the script, where it should release an item after releasing a trigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Valve.VR;
public class Slot : MonoBehaviour
{
public GameObject ItemInSlot;
public Image slotImage;
void Start()
{
slotImage = GetComponentInChildren<Image>();
originalColor = slotImage.color;
}
private void OnTriggerStay(Collider other)
{
if (ItemInSlot != null) return;
GameObject obj = other.gameObject;
if (!IsItem(obj)) return;
if (OVRInput.GetUp(OVRInput.Button.SecondaryHandTrigger))
{
InsertItem(obj);
}
}
bool IsItem(GameObject obj)
{
return obj.GetComponent<Item>();
}
void InsertItem(GameObject obj)
{
obj.GetComponent<Rigidbody>().isKinematic = true;
obj.transform.SetParent(gameObject.transform, true);
obj.transform.localPosition = Vector3.zero;
obj.transform.localEulerAngles = obj.GetComponent<Item>().slotRotation;
obj.GetComponent<Item>().inSlot = true;
obj.GetComponent<Item>().currentSlot = this;
ItemInSlot = obj;
}
}
And in Inventory script I have a problem with adapting this part:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Valve.VR;
public class InventoryVR : MonoBehaviour
{
public GameObject Inventory;
public GameObject Anchor;
bool UIActive;
private void Start()
{
Inventory.SetActive(false);
UIActive = false;
}
private void Update()
{
if (OVRInput.GetDown(OVRInput.Button.Four))
{
UIActive = !UIActive;
Inventory.SetActive(UIActive);
}
}
}
What functions can I use to make this applicable for HTC Vive?
i was just writing my script and this appeared while trying to test the game. heres my script
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class MainMenu : MonoBehaviour
{
[SerializeField] private TMP_Text HighscoreText;
private void Start()
{
int Highscore = PlayerPrefs.GetInt(ScoreSystem.HighscoreKey, 0);
HighscoreText.text = $"High Score: {Highscore}";
}
public void Play()
{
SceneManager.LoadScene(1);
}
}
please help me, tq
The error is very clear: it says that inside the Score System class, it did not find the HighscoreKey variable.
To make this work, if you created the ScoreSystem class yourself, you should add a static string variable called HighscoreKey.
Like this:
public static string HighscoreKey = “…”;
Good Work!
I'm still following a tutorial on making a 2d game in unity. I am working on the GameManager script, but I am getting a error that the namespace LoadSceneMode even though I am coping the code as the guy types it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
private void Awake()
{
instance = this;
}
// Ressources
public List<Sprite> playerSprites;
public List<Sprite> weaponsprites;
public List<int> weaponPrices;
public List<int> xpTable;
// References
public player player;
// public weapon weapon..
//Logic
public int pesos;
public int experience;
// Save state
/*
* INT preferedSkin
* INT pesos
* INT experience
* INT weaponLevel
*/
public void SaveState()
{
string s = "";
s += "0" + "|";
s += pesos.ToString() + '|';
s += experience.ToString() + '|';
s += "0";
PlayerPrefs.SetString("SaveState", s);
}
public void LoadState(Scene s, LoadSceneMode mode)
{
string[] data = PlayerPrefs.GetString("SaveState").Split('|');
// Change player skin
pesos = int.Parse(data[1]);
experience = int.Parse(data[2]);
// Change the weapon level
Debug.Log("LoadState");
}
}
I have tried re typing the script but I am still getting the same error.
It said the error can be found on line (47,36)
You need to add the UnityEngine.SceneManagement namespace to your code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
..........
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;
}
Hello I will actually keep this short.I butchered this piece of code. I can't find the solutions.
My code
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class Linkbutton : MonoBehaviour{
void Update()
{
if (GameObject.activeSelf)
public void LinkFunc();
{
Application.OpenURL ("https://stolpersteinecoevorden.jimdo.com/stolpersteine/"); running = false;
}
}
}
The errors I'm having
I have been trying stuff for hours without a solution in view.
Thanks in advance
Try this:
public class Linkbutton : MonoBehaviour
{
bool running = true;
void Update()
{
if (this.gameObject.activeSelf)
LinkFunc();
}
public void LinkFunc()
{
Application.OpenURL("https://stolpersteinecoevorden.jimdo.com/stolpersteine/");
running = false;
}
}