Computing desk | ||
---|---|---|
< April 21 | << Mar | April | May >> | April 23 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
My daughter is having a problem with an assignment in Unity/C#. This is a two-player game. The arrow keys control the first player and the W-A-S-F keys control the second player. She had this working but she added to the program and the W-A-S-F keys quit working for the second player. I think it must be something simple, but I don't know this language. Can someone help a little?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public Text winLose;
public Text winLoseTwo;
Vector3 startPosition;
float moveHorizontal = 0f;
float moveVertical = 0f;
public AudioClip enemyHit;
public AudioClip collect;
public AudioClip hole;
public AudioClip checkpoint;
private AudioSource source;
private AudioSource hit;
public float hitVol;
public int playerOneScore;
public int playerTwoScore;
public Text pOS;
//pOS will display playerOneScore
public Text pTS;
//pTS will display playerTwoScore
void Start()
{
rb = GetComponent<Rigidbody>();
winLose.text = "";
playerOneScore = 0;
playerTwoScore = 0;
pOS.text = "Player-1's score:" + playerOneScore;
pTS.text = "Player-2's score:" + playerTwoScore;
//this stores the starting position so the players can be sent back if they hit a hole or enemy
startPosition = transform.position;
}
// Update is called once per frame this currently does nothing
void Update () {
}
void FixedUpdate()
{
// the following two lines gets directional input from the keyboard
if (this.gameObject.CompareTag("Player1"))
{
moveHorizontal = Input.GetAxis("Horizontal");
moveVertical = Input.GetAxis("Vertical");
}
else
{
moveHorizontal = Input.GetAxis("Horizontal2");
moveVertical = Input.GetAxis("Vertical2");
}
//this line stores the two lines of input into a single variable called movement
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
pOS.text = "Player-1's score:" + playerOneScore;
pTS.text = "Player-2's score:" + playerTwoScore;
//the following conditional tells the player to move during the game
// this is use to prevent people from moving after the game is done
// so the player will only move if they have not lost
if (winLose.text != "You Lose!! :(")
{
rb.AddForce(movement * speed);
//speed is set in unity so user can decide fast the player can move
}
}
void Awake()
{
source = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Sphere"))
{
// when player touches the golden sphere which is the goal
other.gameObject.SetActive(false);
if (this.gameObject.CompareTag("Player1")){
winLose.text = "You Win!!!"; }
else { winLoseTwo.text = "You Win!!!"; }
// this tell player that they have won
}
if (other.gameObject.CompareTag("Enemy"))
{
//when player collides with enemy (the red guys)
//other.gameObject.SetActive(false);
source.PlayOneShot(enemyHit, hitVol);
//plays a sound when a player collides with an enemy
//transform.position = startPosition;
if (this.gameObject.CompareTag("Player1"))
{
playerOneScore -= 15;
}
else {
playerTwoScore -= 15; }
//players lose 15 points upon touching enemy
}
if (other.gameObject.CompareTag("Hole"))
{
transform.position = startPosition;
//resets the ball position if player touches a hole
//i.e. sends player to start or checkpoint
}
if (other.gameObject.CompareTag("Checkpoint"))
{
startPosition = transform.position;
//this lets the player be sent to the last checkpoint they touched instead of start if they hit a hole
source.PlayOneShot(checkpoint, hitVol);
if (this.gameObject.CompareTag("Player1"))
{
playerOneScore += 10;
}
else
{ playerTwoScore += 10; }
}
//the following if statements tells a player of they win or lose based on whether or not the other player wins or lose
if (winLose.text == "You Win!!!")
{
winLoseTwo.text = "You Lose!! :(";
}
if (winLoseTwo.text == "You Win!!!")
{
winLose.text = "You Lose!! :(";
}
if (winLose.text == "You Lose!! :(")
{
winLoseTwo.text = "You Win!!!";
}
if (winLoseTwo.text == "You Lose!! :(")
{
winLose.text = "You Win!!!";
}
Thank you. Bubba73 You talkin' to me? 02:24, 22 April 2017 (UTC)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public Text winLose;
public Text winLoseTwo;
Vector3 startPosition;
float moveHorizontal = 0f;
float moveVertical = 0f;
public AudioClip enemyHit;
public AudioClip collect;
public AudioClip hole;
// private AudioSource source;
private AudioSource hit;
public float hitVol;
// public int playerOneScore;
// public int playerTwoScore;
// public Text pOS;
// //pOS will display playerOneScore
// public Text pTS;
// //pTS will display playerTwoScore
void Start()
{
rb = GetComponent<Rigidbody>();
winLose.text = "";
// playerOneScore = 0;
// playerTwoScore = 0;
// pOS.text = "Player-1's score:" + playerOneScore;
// pTS.text = "Player-2's score:" + playerTwoScore;
//this stores the starting position so the players can be sent back if they hit a hole or enemy
startPosition = transform.position;
}
// Update is called once per frame this currently does nothing
void Update () {
}
void FixedUpdate()
{
// the following two lines gets directional input from the keyboard
if (this.gameObject.CompareTag("Player1"))
{
moveHorizontal = Input.GetAxis("Horizontal");
moveVertical = Input.GetAxis("Vertical");
}
else
{
moveHorizontal = Input.GetAxis("Horizontal2");
moveVertical = Input.GetAxis("Vertical2");
}
//this line stores the two lines of input into a single variable called movement
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// pOS.text = "Player-1's score:" + playerOneScore;
// pTS.text = "Player-2's score:" + playerTwoScore;
//the following conditional tells the player to move during the game
// this is use to prevent people from moving after the game is done
// so the player will only move if they have not lost
if (winLose.text != "You Lose!! :(")
{
rb.AddForce(movement * speed);
//speed is set in unity so user can decide fast the player can move
}
}
void Awake()
{
source = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Sphere"))
{
// when player touches the golden sphere which is the goal
other.gameObject.SetActive(false);
if (this.gameObject.CompareTag("Player1")){
winLose.text = "You Win!!!"; }
else { winLoseTwo.text = "You Win!!!"; }
// this tell player that they have won
}
if (other.gameObject.CompareTag("Enemy"))
{
//when player collides with enemy (the red guys)
//other.gameObject.SetActive(false);
// source.PlayOneShot(enemyHit, hitVol);
//plays a sound when a player collides with an enemy
// //transform.position = startPosition;
// if (this.gameObject.CompareTag("Player1"))
// {
// playerOneScore -= 15;
// }
// else {
// playerTwoScore -= 15; }
// //players lose 15 points upon touching enemy
}
if (other.gameObject.CompareTag("Hole"))
{
transform.position = startPosition;
//resets the ball position if player touches a hole
//i.e. sends player to start or checkpoint
}
if (other.gameObject.CompareTag("Checkpoint"))
{
startPosition = transform.position;
//this lets the player be sent to the last checkpoint they touched instead of start if they hit a hole
// source.PlayOneShot(checkpoint, hitVol);
// if (this.gameObject.CompareTag("Player1"))
// {
// playerOneScore += 10;
// }
// else
// { playerTwoScore += 10; }
}
//the following if statements tells a player of they win or lose based on whether or not the other player wins or lose
if (winLose.text == "You Win!!!")
{
winLoseTwo.text = "You Lose!! :(";
}
if (winLoseTwo.text == "You Win!!!")
{
winLose.text = "You Lose!! :(";
}
if (winLose.text == "You Lose!! :(")
{
winLoseTwo.text = "You Win!!!";
}
if (winLoseTwo.text == "You Lose!! :(")
{
winLose.text = "You Win!!!";
}
Two obvious first things to check :
ApLundell ( talk) 03:29, 22 April 2017 (UTC)
Oh. I think I have an idea. pOS and pTS are references, but there's no check if they're null. These are variables you've indicated are newly added. Because of the way she's got her scripts set up, she would have to manually set these variables in the editor twice. If she didn't set both of them for each player, it would hit a Null Reference Exception , and bail out of the FixedUpdate function for that player. I'll bet that's it. ApLundell ( talk) 04:12, 22 April 2017 (UTC)
She has player 2 working now. She thinks it was because the text displaying the scores was in FixedUpdate() instead of Update(). Bubba73 You talkin' to me? 15:41, 22 April 2017 (UTC)
<syntaxhighlight lang="c#"></syntaxhighlight>
to produce better looking code in posts like these. I had actually changed it last night and was going to make a note, but I hit an edit conflict and then had to deal with my kids, and then forgot all about it (I just went ahead for the hell of it and so you can see what it looks like with this edit). I'm glad to see the problem got resolved.
ᛗᛁᛟᛚᚾᛁᚱPants
Tell me all about it. 17:40, 22 April 2017 (UTC)I have recently been introduced to public key cryptography. I came across this problem and I'm not sure how to approach it. If I send someone an encrypted message signed with my signature key, what's to prevent them from encrypting it again and sending it to other people pretending the message is directly from me?
Perhaps I haven't gotten my head around how public key signatures work. If my friend sends me a message signed by him, and I forward it to other people, they will think it's from him right? I'm assuming there is something I'm missing to prevent this situation.-- Testing questions ( talk) 15:05, 22 April 2017 (UTC)
Computing desk | ||
---|---|---|
< April 21 | << Mar | April | May >> | April 23 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
My daughter is having a problem with an assignment in Unity/C#. This is a two-player game. The arrow keys control the first player and the W-A-S-F keys control the second player. She had this working but she added to the program and the W-A-S-F keys quit working for the second player. I think it must be something simple, but I don't know this language. Can someone help a little?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public Text winLose;
public Text winLoseTwo;
Vector3 startPosition;
float moveHorizontal = 0f;
float moveVertical = 0f;
public AudioClip enemyHit;
public AudioClip collect;
public AudioClip hole;
public AudioClip checkpoint;
private AudioSource source;
private AudioSource hit;
public float hitVol;
public int playerOneScore;
public int playerTwoScore;
public Text pOS;
//pOS will display playerOneScore
public Text pTS;
//pTS will display playerTwoScore
void Start()
{
rb = GetComponent<Rigidbody>();
winLose.text = "";
playerOneScore = 0;
playerTwoScore = 0;
pOS.text = "Player-1's score:" + playerOneScore;
pTS.text = "Player-2's score:" + playerTwoScore;
//this stores the starting position so the players can be sent back if they hit a hole or enemy
startPosition = transform.position;
}
// Update is called once per frame this currently does nothing
void Update () {
}
void FixedUpdate()
{
// the following two lines gets directional input from the keyboard
if (this.gameObject.CompareTag("Player1"))
{
moveHorizontal = Input.GetAxis("Horizontal");
moveVertical = Input.GetAxis("Vertical");
}
else
{
moveHorizontal = Input.GetAxis("Horizontal2");
moveVertical = Input.GetAxis("Vertical2");
}
//this line stores the two lines of input into a single variable called movement
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
pOS.text = "Player-1's score:" + playerOneScore;
pTS.text = "Player-2's score:" + playerTwoScore;
//the following conditional tells the player to move during the game
// this is use to prevent people from moving after the game is done
// so the player will only move if they have not lost
if (winLose.text != "You Lose!! :(")
{
rb.AddForce(movement * speed);
//speed is set in unity so user can decide fast the player can move
}
}
void Awake()
{
source = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Sphere"))
{
// when player touches the golden sphere which is the goal
other.gameObject.SetActive(false);
if (this.gameObject.CompareTag("Player1")){
winLose.text = "You Win!!!"; }
else { winLoseTwo.text = "You Win!!!"; }
// this tell player that they have won
}
if (other.gameObject.CompareTag("Enemy"))
{
//when player collides with enemy (the red guys)
//other.gameObject.SetActive(false);
source.PlayOneShot(enemyHit, hitVol);
//plays a sound when a player collides with an enemy
//transform.position = startPosition;
if (this.gameObject.CompareTag("Player1"))
{
playerOneScore -= 15;
}
else {
playerTwoScore -= 15; }
//players lose 15 points upon touching enemy
}
if (other.gameObject.CompareTag("Hole"))
{
transform.position = startPosition;
//resets the ball position if player touches a hole
//i.e. sends player to start or checkpoint
}
if (other.gameObject.CompareTag("Checkpoint"))
{
startPosition = transform.position;
//this lets the player be sent to the last checkpoint they touched instead of start if they hit a hole
source.PlayOneShot(checkpoint, hitVol);
if (this.gameObject.CompareTag("Player1"))
{
playerOneScore += 10;
}
else
{ playerTwoScore += 10; }
}
//the following if statements tells a player of they win or lose based on whether or not the other player wins or lose
if (winLose.text == "You Win!!!")
{
winLoseTwo.text = "You Lose!! :(";
}
if (winLoseTwo.text == "You Win!!!")
{
winLose.text = "You Lose!! :(";
}
if (winLose.text == "You Lose!! :(")
{
winLoseTwo.text = "You Win!!!";
}
if (winLoseTwo.text == "You Lose!! :(")
{
winLose.text = "You Win!!!";
}
Thank you. Bubba73 You talkin' to me? 02:24, 22 April 2017 (UTC)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public Text winLose;
public Text winLoseTwo;
Vector3 startPosition;
float moveHorizontal = 0f;
float moveVertical = 0f;
public AudioClip enemyHit;
public AudioClip collect;
public AudioClip hole;
// private AudioSource source;
private AudioSource hit;
public float hitVol;
// public int playerOneScore;
// public int playerTwoScore;
// public Text pOS;
// //pOS will display playerOneScore
// public Text pTS;
// //pTS will display playerTwoScore
void Start()
{
rb = GetComponent<Rigidbody>();
winLose.text = "";
// playerOneScore = 0;
// playerTwoScore = 0;
// pOS.text = "Player-1's score:" + playerOneScore;
// pTS.text = "Player-2's score:" + playerTwoScore;
//this stores the starting position so the players can be sent back if they hit a hole or enemy
startPosition = transform.position;
}
// Update is called once per frame this currently does nothing
void Update () {
}
void FixedUpdate()
{
// the following two lines gets directional input from the keyboard
if (this.gameObject.CompareTag("Player1"))
{
moveHorizontal = Input.GetAxis("Horizontal");
moveVertical = Input.GetAxis("Vertical");
}
else
{
moveHorizontal = Input.GetAxis("Horizontal2");
moveVertical = Input.GetAxis("Vertical2");
}
//this line stores the two lines of input into a single variable called movement
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// pOS.text = "Player-1's score:" + playerOneScore;
// pTS.text = "Player-2's score:" + playerTwoScore;
//the following conditional tells the player to move during the game
// this is use to prevent people from moving after the game is done
// so the player will only move if they have not lost
if (winLose.text != "You Lose!! :(")
{
rb.AddForce(movement * speed);
//speed is set in unity so user can decide fast the player can move
}
}
void Awake()
{
source = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Sphere"))
{
// when player touches the golden sphere which is the goal
other.gameObject.SetActive(false);
if (this.gameObject.CompareTag("Player1")){
winLose.text = "You Win!!!"; }
else { winLoseTwo.text = "You Win!!!"; }
// this tell player that they have won
}
if (other.gameObject.CompareTag("Enemy"))
{
//when player collides with enemy (the red guys)
//other.gameObject.SetActive(false);
// source.PlayOneShot(enemyHit, hitVol);
//plays a sound when a player collides with an enemy
// //transform.position = startPosition;
// if (this.gameObject.CompareTag("Player1"))
// {
// playerOneScore -= 15;
// }
// else {
// playerTwoScore -= 15; }
// //players lose 15 points upon touching enemy
}
if (other.gameObject.CompareTag("Hole"))
{
transform.position = startPosition;
//resets the ball position if player touches a hole
//i.e. sends player to start or checkpoint
}
if (other.gameObject.CompareTag("Checkpoint"))
{
startPosition = transform.position;
//this lets the player be sent to the last checkpoint they touched instead of start if they hit a hole
// source.PlayOneShot(checkpoint, hitVol);
// if (this.gameObject.CompareTag("Player1"))
// {
// playerOneScore += 10;
// }
// else
// { playerTwoScore += 10; }
}
//the following if statements tells a player of they win or lose based on whether or not the other player wins or lose
if (winLose.text == "You Win!!!")
{
winLoseTwo.text = "You Lose!! :(";
}
if (winLoseTwo.text == "You Win!!!")
{
winLose.text = "You Lose!! :(";
}
if (winLose.text == "You Lose!! :(")
{
winLoseTwo.text = "You Win!!!";
}
if (winLoseTwo.text == "You Lose!! :(")
{
winLose.text = "You Win!!!";
}
Two obvious first things to check :
ApLundell ( talk) 03:29, 22 April 2017 (UTC)
Oh. I think I have an idea. pOS and pTS are references, but there's no check if they're null. These are variables you've indicated are newly added. Because of the way she's got her scripts set up, she would have to manually set these variables in the editor twice. If she didn't set both of them for each player, it would hit a Null Reference Exception , and bail out of the FixedUpdate function for that player. I'll bet that's it. ApLundell ( talk) 04:12, 22 April 2017 (UTC)
She has player 2 working now. She thinks it was because the text displaying the scores was in FixedUpdate() instead of Update(). Bubba73 You talkin' to me? 15:41, 22 April 2017 (UTC)
<syntaxhighlight lang="c#"></syntaxhighlight>
to produce better looking code in posts like these. I had actually changed it last night and was going to make a note, but I hit an edit conflict and then had to deal with my kids, and then forgot all about it (I just went ahead for the hell of it and so you can see what it looks like with this edit). I'm glad to see the problem got resolved.
ᛗᛁᛟᛚᚾᛁᚱPants
Tell me all about it. 17:40, 22 April 2017 (UTC)I have recently been introduced to public key cryptography. I came across this problem and I'm not sure how to approach it. If I send someone an encrypted message signed with my signature key, what's to prevent them from encrypting it again and sending it to other people pretending the message is directly from me?
Perhaps I haven't gotten my head around how public key signatures work. If my friend sends me a message signed by him, and I forward it to other people, they will think it's from him right? I'm assuming there is something I'm missing to prevent this situation.-- Testing questions ( talk) 15:05, 22 April 2017 (UTC)