- Home /
Need help accessing booleans from other script.
I'm making an endless runner game where platforms move towards the player so it makes it seem like the player is running across them. I'm trying to make it so that when the player dies, the platforms stop moving. What I have isn't working, when the player dies, the platforms just keep moving as if nothing has happened. Here is the script I have that is attached to the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CoinColliderScript : MonoBehaviour
{
void OnTriggerEnter(Collider target)
{
if (target.tag == "Enemy")
{
StartCoroutine(Died());
}
}
IEnumerator Died()
{
PlatformMover.dead = true;
gameObject.GetComponentInParent<Animator>().Play("Male Died");
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene("MainMenu");
}
}
Here is the script I have that is attached to the platforms: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlatformMover : MonoBehaviour
{
private Vector3 newPlatPos;
public float platMoveSpeed;
public static bool dead;
public void Start()
{
dead = false;
}
void Update()
{
if (dead)
{
newPlatPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
} else if (!dead)
{
newPlatPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - platMoveSpeed);
}
transform.position = newPlatPos;
}
}
I know that the "OnTriggerEnter" and the Coroutine are working because the "Male Died" animation plays and the Scene switches to the MainMenu scene. What do I do to fix this? Thank you for helping me!
Answer by biT1249 · Jul 21, 2020 at 03:46 AM
I am a beginner with unity, but i think I know what you need to do... You need to reference your player to the script.
//the Player is your player in your game.
public Player player;
//so if your player name is Runner, then you put the second name as that and it will recognize it. Then after you save it, you have to go to the editor and drag in the player into the box that will appear in the script you are referencing to, which is PlatformMover. Sorry if it is confusing.. oh, and the third name is just so that in the script you call that name and then ref it like this: player.deathBool, or what i would do since you are trying to stop the platforms put it in a if function. sorry again..
Make the code which in this case is a bool that you need to reference public in the Player script.
public deathBool = false;
or whatever your bool is called. Sorry if this is confusing... Hope this helps! :)
Little hint: Since the variable dead
is in your platform$$anonymous$$over you need to reference to that from your player Skript. But beside that this is the way to go. puplic Platform$$anonymous$$over mover
I missed the bool, it was late and i was tired so yeah.. Thanks for commenting that!
Answer by UnityToMakeMoney · Jul 21, 2020 at 06:15 AM
There are two options here
Easy: make everything stop
Difficult: Get the components and get the variable of that component
For the easy method just add this code after player dies
Time.timScale = 0f;
For the difficult method this is what you have to do:
Create a game object variable
public GameObject objWithScript;//in the Inspector drop gamobject with other script
Create another variable, but this time with the type of the other script like this:
//if you want to access the platform mover script public PlatformMover pm;//short for Platform Mover //if you want to access the coin collider script public CoinColliderScript ccs;//short for Coin Collider Script
Then assign the script variable as such:
void Start(){ //if accessing Platform Mover pm = objWithScript.GetComponent<PlatformMover>(); //if accessing CoinCollider Script ccs = objWithScript.GetComponent<CoinColliderScript>(); }
Make sure that the variable you want to access from the other script is public and then do the following:
//replace variableName with the actual name of the variable //if getting variable from Platform Mover Debug.Log(pm.variableName);//debugs the value of that variable //if getting variable from Coin Collision Script Debug.Log(ccs.variableName);//debugs the value of that variable
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
SpeedBoost not working,My speedBoostTaken variable isn't working, why? 1 Answer
I can't figure out why this if statement isn't responding 1 Answer
How to change a Bool for only a single prefab GameObject? 1 Answer
Setting bool from other script for specific gameobjects (while they're using same movement script) 2 Answers