Unity 3d doesnt not contain a definition - unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercontroler : MonoBehaviour {
public static playercontroler sharedinstance;
void Awake(){
sharedinstance = this;
rigidBody = GetComponent<Rigidbody2D> ();
}
public void KillPlayer(){
GameManager.sharedistance.GameOver ();
animator.SetBool ("isAlive", false);
}
}
and
public class killtriger : MonoBehaviour {
void OnTriggerEnter2D(Collider2D theObject){
if (theObject.tag == "Player") {
playercontroler.sharedinstance.KillPlayer ();
}
}
}
the problem is that unity return:
"playercontroler" doesnt not contain a definition for sharedinstance
what is the problem? thanks

Don't know if that will fix everything, but you should check if playercontroler.sharedinstance have been set before using it.
public class killtriger : MonoBehaviour {
void OnTriggerEnter2D(Collider2D theObject){
if (theObject.tag == "Player" && playercontroler.sharedinstance) {
playercontroler.sharedinstance.KillPlayer ();
}
}
}
Also make sure the variable name is right. You have the variables sharedistance and sharedinstance in your code. They are for different objects, but I imagine they should have the same name.

Related

Creating an Inventory in Unity 3D - adapting from Oculus to HTC

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?

Unity The name 'PrefabUtility' does not exist in the current context

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.

what to do when you won't save

when I load a new scene I get this error: NullReferenceException: Object reference not set to an instance of an object GameController+d__16.MoveNext () (at Assets/Scrips/GameController.cs:76) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at :0)
this is my script to save:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Scene_Manager : MonoBehaviour
{
int Saved_scene;
int Scene_index;
public void Load_Saved_Scene()
{
Saved_scene = PlayerPrefs.GetInt("Saved");
if (Saved_scene != 2)
SceneManager.LoadSceneAsync(Saved_scene);
else
return;
}
public void Save_and_Exit()
{
Scene_index = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("Saved", Scene_index);
PlayerPrefs.Save();
SceneManager.LoadSceneAsync(0);
}
public void Next_Scene()
{
Scene_index = SceneManager.GetActiveScene().buildIndex + 1;
SceneManager.LoadSceneAsync(Scene_index);
}
}
and this is my script for the whole game where I call save:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(GameUI))]
public class GameController : MonoBehaviour
{
public static GameController Instance { get; private set; }
[SerializeField]
private int knifeCount;
[Header("Knife Spawning")]
[SerializeField]
private Vector2 knifeSpawnPosition;
[SerializeField]
private GameObject knifeObject;
public GameUI GameUI { get; private set; }
private void Awake()
{
Instance = this;
GameUI = GetComponent<GameUI>();
}
private void Start()
{
GameUI.SetInitialDisplayedKnifeCount(knifeCount);
SpawnKnife();
}
public void OnSuccessfulKnifeHit()
{
if (knifeCount > 0)
{
SpawnKnife();
}
else
{
StartGameOverSequence(true);
}
}
private void SpawnKnife()
{
knifeCount--;
Instantiate(knifeObject, knifeSpawnPosition, Quaternion.identity);
}
public void StartGameOverSequence(bool win)
{
StartCoroutine("GameOverSequenceCoroutine", win);
}
private IEnumerator GameOverSequenceCoroutine(bool win)
{
if (win)
{
yield return new WaitForSecondsRealtime(0.3f);
FindObjectOfType<LevelLoader>().LoadNextLevel();
FindObjectOfType<Scene_Manager>().Save_and_Exit();
}
else
{
GameUI.ShowRestartButton();
}
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
}
}
Now first of all take a look at this post to understand NullReferenceExceptions.
After that take a closer look at your error message:
NullReferenceException: Object reference not set to an instance of an object GameController+d__16.MoveNext () (at Assets/Scrips/GameController.cs:76)
The (at Assets/Scrips/GameController.cs:76) part tells you exactly where you error is thrown (which basically means where it occured). It's in your Assets/Scrips/GameController.cs script at line 76.
private IEnumerator GameOverSequenceCoroutine(bool win)
{
if (win)
{
yield return new WaitForSecondsRealtime(0.3f);
FindObjectOfType<LevelLoader>().LoadNextLevel();
FindObjectOfType<Scene_Manager>().Save_and_Exit(); // <--- HERE
}
...
}
In your specific implementation my best guess would be that you shouldn't load your new scene before you try to save & quit. You're starting to load your new scene asynchronously and then call another function which loads another scene (?) while not actually preventing the first scene from activating until your other functions actually executed properly. There's a plethora of cases where this can and will break.
I think you'll need to rethink what it is your actually trying to accomplish because these function calls don't make sense like that.

Custom onClick list

Can i create custom region with grouped methods for list onClick like dynamic and statics?
like this
Yes and no! ^^
Yes, you can create your own event type taking a parameter and assign dynamic callbacks to it. What you are looking for is UnityEvent.
For the dynamic parameterized ones see UnityEvent<T0> to UnityEvent<T0, T1, T2, T3> depending on how many parameters you need.
For the example with a single int it would be (exactly as in the API example)
// Since Unity doesn't support direct serialization of generics you have to implement this [Serializable] wrapper
[Serializable]
public class MyIntEvent : UnityEvent<int>
{
}
public class ExampleClass : MonoBehaviour
{
public MyIntEvent m_MyEvent;
}
No, you can not simply change the existing implementation of UI.Button.onClick which is parameterless.
What you could do, however, is build a new component and attach it on a button like
[RequireComponent(typeof(Button))]
public class ExampleClass : MonoBehaviour
{
[SerializeField] private Button _button;
public MyIntEvent onClickWithIntParameter;
private void Awake()
{
if(!_button) _button = GetComponent<Button>();
_button.onClick.AddListener(HandleOnClick);
}
private void HandleOnClick()
{
// Wherever you get your int from
var value = 123;
onClickWithIntParameter.Invoke(value);
}
}
In the case that [Serializable] isn't working for you try [System.Serializable] or using System; at the top.
For Those who want to create a custom event Script:
I made a collision detection script. The events set in the inspector are called when a collision is detected. Just like a button.
https://i.stack.imgur.com/r4epP.png
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Events;
public class CollisionCallFunc : MonoBehaviour {
[System.Serializable]
public class CollisionEvent : UnityEvent<object> {
public object value;
}
[SerializeField]
private CollisionEvent collisionEvents = new();
private void OnCollisionEnter(Collision collision) {
try {
collisionEvents.Invoke(collisionEvents.value);
} catch(System.Exception exception) {
Debug.LogWarning("Couldn't invoke action. Error:");
Debug.LogWarning(exception.Message);
}
}
private void OnCollisionEnter2D(Collision2D collision) {
try {
collisionEvents.Invoke(collisionEvents.value);
} catch(System.Exception exception) {
Debug.LogWarning("Couldn't invoke action. Error:");
Debug.LogWarning(exception.Message);
}
}
}

Unity monodevelop, if activate gameobject run script

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;
}
}