Can't pickup sphere with Vive controller. Collision not detected - c#

I am doing a tutorial from VR Dev School. The lesson is Picking Up an Object and Parent the Transform. This is the code I copied exactly from the lesson. I have the script and a sphere collider attached to controlller(left). I've tried toggling 'is trigger' on/off. The collision is not being detected in the console. I am not receiving any errors or warnings.
Any assistance is appreciated and I will answer any questions
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class PickupParent : MonoBehaviour {
SteamVR_TrackedObject trackedObj;
SteamVR_Controller.Device device;
void Awake () {
trackedObj = GetComponent<SteamVR_TrackedObject>();
}
void FixedUpdate () {
device = SteamVR_Controller.Input((int)trackedObj.index);
if(device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You are holding 'Touch' on the trigger");
}
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You activated touchdown on the trigger");
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You activated TouchUp on the trigger");
}
if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You are holding 'Press' on the trigger");
}
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You activated press down on the trigger");
}
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You activated press Up on the trigger");
}
}
void onTriggerStay(Collider col)
{
Debug.Log("You have collided with " + col.name + " and activated onTriggerStay");
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You have collided with " + col.name + " while holding down Touch");
col.attachedRigidbody.isKinematic = true;
col.gameObject.transform.SetParent(gameObject.transform);
}
}
}

This is a simple mistake. It should be OnTriggerStay not onTriggerStay. Please capitalize the O and trigger/collision should be detected.

Related

OnTriggerExit and OnTriggerEnter not working

I have a problem. I want isOffRoad to become true whenever the wheel enters a collider attached to an object with a tag offRoadBorder, and for it to become false upon exiting it. Nothing seems to work. Both collider and the wheel have rigidbody2D attached. Thanks in advance.
public bool isOffRoad = false;
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log("a");
if (col.tag == "offRoadBorder")
{
isOffRoad = true;
}
}
void OnTriggerExit2D(Collider2D col)
{
Debug.Log("b");
if (col.tag == "offRoadBorder")
{
isOffRoad = false;
}
}
Check IsTrigger field in collider componenets. Triggers are using for physically empty colliders that have script uses.
Otherwise use OnCollision2D:
void OnCollisionEnter2D(Collision2D col)
{
if (col.transform.CompareTag("offRoadBorder")) isOffRoad = true;
}
void OnCollisionExit2D(Collider2D col)
{
if (col.transform.CompareTag("offRoadBorder")) isOffRoad = false;
}
Make sure that they both have a collider and that both colliders have the "Is Trigger" enabled.

unity Photon OnTriggerStay2D only works for host

im adding perks (upgrades) to my game but they only work properly when you play alone, when multiple people are in the game, the functionality stops working.
when the player 1 (host) enters the trigger, a pop up message appears saying Press F to purchase etc etc.. but when player 2 enters the trigger, the message does not appear on his screen but on player 1's screen.
script:
public void Update()
{
if(!view.IsMine) return;
if(Input.GetKey(KeyCode.F))
{
isPressingF = true;
}
else
{
isPressingF = false;
}
}
void OnTriggerStay2D (Collider2D collider)
{
if(!view.IsMine) return;
if (!ownedHalet)
{
if(collider.gameObject.CompareTag("Player"))
{
juggConfirm.SetActive(true);
if (isPressingF)
{
if(PointSystem.Instance.points < 2500 )
return;
BuyPerkHalet();
}
}
}
}
```

The object of type Text has been destroyed but you are still trying to access it - Unity

I have a game that has a main play scene and a game over scene. The Game over scene has a button that simply starts the main scene again.
Everything works fine for the first play, but when restarting through the game over screen and trying to add to my score I get the following error:
The object of type Text has been destroyed but you are still trying to access it.
I'm not sure why this is, I've tried logging out the referenced text object in the update and it looks fine.
I'm setting the highScore by dragging the text UI component to the script in the editor. I've also tried using GetComponent but run into the same issue.
Heres the relevant script:
public class ScoreHandler : MonoBehaviour
{
public Text highScore;
private int score = 0;
private int basicEnemyValue = 10;
private int speedyEnemnyValue = 15;
private int bossEnemyValue = 50;
private int startingHighScore;
public static event Action<int> OnScoreChange;
void OnEnable() {
BasicEnemy.OnBasicEnemyDestroyed += BasicEnemyDestroyed;
TankEnemy.OnTankEnemyDestroyed += TankEnemyDestroyed;
SpeedyEnemy.OnSpeedyEnemyDestroyed += SpeedyEnemyDestroyed;
}
void onDisable() {
BasicEnemy.OnBasicEnemyDestroyed -= BasicEnemyDestroyed;
TankEnemy.OnTankEnemyDestroyed -= TankEnemyDestroyed;
SpeedyEnemy.OnSpeedyEnemyDestroyed -= SpeedyEnemyDestroyed;
}
void Start () {
startingHighScore = PlayerPrefs.GetInt ("highscore", 0);
highScore.text = "Score: " + score;
}
void Update () {
Debug.Log(highScore); // Looks good here, has a ref to the correct component UnityEngine.Ui.Text
Debug.Log(highScore.text); // Same as above - has what's set in start
}
private void checkHighScore(int score) {
if (score > startingHighScore) {
PlayerPrefs.SetInt ("highscore", score);
}
}
// ***HERE IS THE PROBLEM FUNCTION***
private void updateScoreText() {
Debug.Log(highScore); // null
highScore.text = "Score: " + score;
checkHighScore(score);
OnScoreChange?.Invoke(score);
}
private void BasicEnemyDestroyed() {
score += basicEnemyValue;
updateScoreText();
}
private void TankEnemyDestroyed() {
score += bossEnemyValue;
updateScoreText();
}
private void SpeedyEnemyDestroyed() {
score += speedyEnemnyValue;
updateScoreText();
}
}
I'm a bit confused as everything looks fine in the update, but seems to loose the component when I call the function on it. I've tried to add dontDestoryOnLoad() to the UI, but that adds a number of other issues with other UI components not being defined.
onDisable -> OnDisable
It's case-sensitve, so your event handlers are never removed.
FYI there is a shortcut to insert these messages in Visual Studio: CtrlShiftM

The object of type 'GameObject' has been destroyed but you are still trying to access it

So I am working on a runner game and it is almost done. The problem is that I am testing the Pause Panel and when player touches a zombie, pause panel shows up and I am restarting the game again by pressing the restart button. But when I touch the zombie again panel doesnt show up and gives me the error in the title. I am stuck and any help will be appreciated. This is the code and I referenced to the line that error sends me :
[SerializeField]
private GameObject pausePanel;
[SerializeField]
private Button RestartGameButton;
[SerializeField]
private Text ScoreText;
private int score;
void Start ()
{
pausePanel.SetActive(false);
ScoreText.text = score + "M";
StartCoroutine(CountScore());
}
IEnumerator CountScore()
{
yield return new WaitForSeconds(0.6f);
score++;
ScoreText.text = score + "M";
StartCoroutine(CountScore());
}
void OnEnable()
{
PlayerDeath.endgame += PlayerDiedEndTheGame;
}
void OnDisable()
{
PlayerDeath.endgame += PlayerDiedEndTheGame;
}
void PlayerDiedEndTheGame()
{
if (!PlayerPrefs.HasKey("Score"))
{
PlayerPrefs.SetInt("Score", 0);
}
else
{
int highscore = PlayerPrefs.GetInt("Score");
if(highscore < score)
{
PlayerPrefs.SetInt("Score", score);
}
}
pausePanel.SetActive(true); //this is the line that error sends me but I cant figure it out because I didnt try to destroy the panel in the first place.
RestartGameButton.onClick.RemoveAllListeners();
RestartGameButton.onClick.AddListener(() => RestartGame());
Time.timeScale = 0f;
}
public void PauseButton()
{
Time.timeScale = 0f;
pausePanel.SetActive(true);
RestartGameButton.onClick.RemoveAllListeners();
RestartGameButton.onClick.AddListener(() => ResumeGame());
}
public void GoToMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("MainMenu");
}
public void ResumeGame()
{
Time.timeScale = 1f;
pausePanel.SetActive(false);
}
public void RestartGame()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Gameplay");
}
I found the solution. It was a simple mistake on the OnDisable method. I deleted the += sign and changed it with -= sign because it was enabling the event when it was supposed to disable.

Converting mouse position functionality to work with touchscreen

I'm developing in Unity, C#, and I have a bit of code that checks for player activity based on mouse position and it's working well but I'm needing to also check player activity on a touchscreen (not a mobile phone touchscreen, but a touchscreen attached to a pc). How should I modify the code that I have below to also work with touch?
private void Start()
{
InvokeRepeating("LastMousePosition", 0, _checkMousePositionTimingInterval);
}
private void Update()
{
_currentMousePosition = Input.mousePosition;
}
void LastMousePosition()
{
_prevMousePosition = Input.mousePosition;
}
void CheckPlayerIdle()
{
if (_currentMousePosition != _prevMousePosition)
UserActive = true;
else if (_currentMousePosition == _prevMousePosition)
UserActive = false;
}
Well for touch you start your inactivity timer if you don't get any touch inputs and wait for x duration.
for eg:
private void Update()
{
if(Input.touchCount == 0)
{
if(!checkingForInactivity)
{
checkingForInactivity = true;
myRoutine = StartCoroutine(CheckForInactivity());
}
}
else
{
if(checkingForInactivity) StopCoroutine(myRoutine);
}
}
Ienumrable CheckForInactivity()
{
yield new waitForSecond(3.0f);
//user is inactive
}
}

Categories