I feel like this is a poor question to ask, but I couldn't figure out what the issue was.
I was making a ScoreController and I wanted to make it so that a coin object, when hit, would add 100 to the score GUI. All the coins have this script inside of them:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreController : MonoBehaviour
{
public void OnTriggerEnter2D(Collider2D collider)
{
transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
}
}
I can recognize that this is a very poor way of getting an object but I can't figure out what the issue is. Here is the script that I was trying to reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreController : MonoBehaviour
{
public static int score = 0; //Number of coins
public static GameObject scoreUI; //A gameObject for the CoinUI
// Update is called once per frame
void Start()
{
scoreUI = GameObject.Find("ScoreUI"); //Automatically sets all coins to have their coinUI GameObject equal the CoinUI UI.
}
public void CollectCoin() //Runs when a coin is collected. Makes it disapear and adds one to the counter.
{
gameObject.SetActive(false);
score += 100;
scoreUI.GetComponent<Text>().text = "Score: " + score.ToString();
}
public void KilledMonster()
{
score += 1000;
}
}
The ScoreControllerUI is inside Canvas so I don't think I have the wrong path. Can you not transform.Find() from the scene? Here is my scene:
As I am sure you can tell, I am new to this site and to Unity, so I apologize for any mistakes in the way I asked this question. Feel free to give me constructive criticism on that as well. I am using unity version 2020.3.24f1 and coding in C#. I don't know if this is relevant but I am coding using visual studio.
Sorry again for any dumb mistakes I made.
Instead of using
transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
use
GameObject.Find("Canvas/ScoreUIController").GetComponent<ScoreController>().CollectCoin();
Suggestion: Define a public ScoreController ScoreUIController in your CoinScoreController (I'm assuming your coin is a prefab) then drag and drop the ScoreUIController into it to assign your script to our defined variable.
Now, you would be able to call
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreController : MonoBehaviour
{
public ScoreController ScoreUIController;
public void OnTriggerEnter2D(Collider2D collider)
{
ScoreUIController.CollectCoin();
}
}
P.S. : Please follow tutorials and read documentation first, as I can see your code is very process heavy.
Related
I want that whenever my player passes through a particular portion of my obstacle it should add 2 points to the score. In order to do this I've made a child of the obstacle. This child contains the box collider which covers that particular portion of the obstacle (I've switched on the Is Trigger in Unity).
Code on child having trigger -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Score : MonoBehaviour
{
float points;
void Start()
{
}
void Update()
{
Debug.Log(points);
}
void OnTriggerExit2D(Collider2D other)
{
points += 2f;
}
}
The problem is that in the console the points are showing 0s and 2s only like this -
Console
While it should be 0, 2, 4, 6... after passing the obstacle.
Also clones of the original obstacle are being created, i.e. I pass through a new clone each time; in case this is causing the problem.
Yeah it is obvious that it will print out 0s and 2s because your script doesn't use a centralized scoring system it just updates the score in that particular instance. You can make another script call it GameManager where you can make a player score variable and on trigger exit function of your child function would increment the score of that GameManager variable playerscore.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public float playerPoints = 0f;
}
In Score Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Score : MonoBehaviour
{
public GameManager manager; // Drag and drop the gamemanager object from scene in to this field
void OnTriggerExit2D(Collider2D other)
{
manager.playerPoints += 2f;
Debug.Log(manager.playerPoints);
}
}
My code doesn't work. The player is supposed to respawn when a player with the tag "Player" interacts with an object with this code. Can someone help me, thanks in advance! Code ☟
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class KillPlayerLava : MonoBehaviour
{
public int Respawn;
public float Seconds;
public MonoBehaviour script;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public IEnumerator OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
yield return new WaitForSeconds(Seconds);
SceneManager.LoadScene(Respawn);
script.enabled = false;
}
}
}
Here are something which might help you find the answer, the code you posted looks fine but there can be other things causing the issue.
Does your gameobject have a 2D collider?
Does one of the objects that it's colliding with have a Rigidbody2D?
Note: Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached. Trigger events are sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.
Source docs : MonoBehaviour.OnTriggerEnter2D(Collider2D)
Does your play have the Player tag set?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
the stuff at the top is just stuff unity adds i am just adding this because my post has too much code apparently
void Update()
{
This tells it to print hi in the console
if (Input.GetKeyDown(KeyCode.Space)) ;
{
Debug.Log("hi.");
}
}
}
hmm, may be your problem is in your code, just clear the ; element
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("hi.");
}
}
The compiler will show you a red error and also in unity editor.
If you have done my instruction but still can not do it, create a gameobject in the hierarchy and drag the script you have written to the gameobject
I am making a VR game where I want to display the ammo next to the gun. I keep getting a error and dont know how to fix it. Please help
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ammoText : MonoBehaviour
{
public GameObject weapon;
Text ammoText;
void Awake()
{
ammoText = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
ammoText.text = weapon.GetComponent<Gun>().currentBullets.ToString();
}
}
After passing line public class ammoText : MonoBehaviour the compiler knows that ammoText is a class and this is used to evaluate the correctness of the code. Statement Text ammoText; generates a compilation error because class ammoText must not occur in such a statement. Also other occurrences of ammoText will generate errors.
It is recommended to start class names with capital letters so it may be enough to rename the class to AmmoText.
You can't name a variable the same as your class name. Change the text field name from ammoText to something else.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ammoText : MonoBehaviour
{
public GameObject weapon;
Text renameThis;
void Awake()
{
renameThis = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
renameThis.text = weapon.GetComponent<Gun>().currentBullets.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Q : MonoBehaviour
{
void OnTriggerStay2D(Collider2D collision)
{
if (Input.GetKeyDown("q"))
{
Destroy(collision.gameObject);
}
}
}
ok so i wrote code that is supposed to detect when another object is colliding with it to allow the player to press a key to destroy the collider. the code is able to detect a collider but is not able to detect a key press while the collider is being detected. I have zero clue to as why this is happening so if anybody can help that would be great thank you
If you look at the documentation for GetKeyDown, you'll see that it needs to be in your Update callback. This would be super easy to fix though!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Q : MonoBehaviour
{
bool _qPressed;
void Update()
{
_qPressed = Input.GetkeyDown("q");
}
void OnTriggerStay2D(Collider2D collision)
{
if (_qPressed)
{
Destroy(collision.gameObject);
}
}
}