I tried multiple times, with GetComponet and public calling said script, but i cannot access the public bools from this other scripts
Perhaps share your code and somebody can help.
There are a few ways to check variables “across” scripts. You can use public variables:
public bool SomePublicBoolean = false;
Or you could have Getter/Setter style methods maybe:
private bool somePrivateBoolean = true;
public bool GetSomeBoolean()
{
return somePrivateBoolean;
}
And from another script you “get” that back from the other script, by accessing it like this:
localBoolean = SomeOtherScript.SomePublicBoolean;
otherLocalBoolean = SomeOtherScript.GetSomeBoolean();
And these are only just a couple simple ways, there are other ways too. If I knew what your code looked like maybe I could help more.
If your having problems with having a reference to that other script, you could try a quick dirty fix, which I can’t recommend for any serious use setup like this… and should only be used with scripts that are used as singletons, where there is only a single one of those scripts ever active at once (like a manager object, rather than a script that is attached to every enemy or something)
But anyway if you create a static instance of a script like this:
public static SomeScriptName Instance;
void Awake()
{
Instance = this;
}
Then you can later access that script from other scripts, by doing this:
someLocalBoolean = SomeScriptName.Instance.GetSomeBoolean();
Andddddd… if you need to have many scripts attached to many objects, then perhaps you should consider something else to access them, like worst case scenario is to use getcomponent a lot, because it can be slow when used frequently. A better solution would be some kind of manager that keeps a reference to all the “many scripts” from one script, and you use it sort of as a message center to communicate from all scripts to the “master” script.
Hope that helps! Good luck!
using UnityEngine;
using System.Collections;
using UnityStandardAssets;
namespace LUKEWARM
{
public class SuperHot_TimeController : MonoBehaviour
{
public float slowDown = 0.05f;
public GameObject player;
void Start()
{
SetInitialReferences();
}
void Update()
{
SlowMotion();
}
void SlowMotion()
{
Time.timeScale = slowDown;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
}
void SetInitialReferences()
{
PlayerMovement playerMovement = player.GetComponent<PlayerMovement>();
}
}
}
using UnityEngine;
using System.Collections;
namespace LUKEWARM
{
public class PlayerMovement : MonoBehaviour
{
private TimeController timeController;
//The letter in front of the bools are the keys on the keyboard
public bool a_HorizontalMovement = false;
public bool d_HorizontalMovement = false;
public bool w_VerticalMovement = false;
public bool s_VerticalMovement = false;
// Use this for initialization
void Start()
{
timeController = FindObjectOfType(typeof(TimeController)) as TimeController;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
if (!timeController.isReversing)
{
HorizontalMovement();
VerticalMovement();
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
}
//make a better movement engine down here |
// V
public void VerticalMovement()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * 3.0f * Time.deltaTime);
w_VerticalMovement = true;
}
else
{
w_VerticalMovement = false;
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back * 3.0f * Time.deltaTime);
s_VerticalMovement = true;
}
else
{
s_VerticalMovement = false;
}
}
public void HorizontalMovement()
{
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * 3.0f * Time.deltaTime);
a_HorizontalMovement = true;
}
else
{
a_HorizontalMovement = false;
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * 3.0f * Time.deltaTime);
d_HorizontalMovement = true;
}
else
{
d_HorizontalMovement = false;
}
}
}
}
This is the two codes that i am working in, but i cannot get the public bools from the PayerMovement.cs
Your SetInitialReferences doesn’t really do anything…It creates a method variable which means it can’t be used outside that method. Within that method, you should be able to access playerMovement.a_HorizontalMovement. If you plan to access it outside of that method, you’ll need the PlayerMovement playerMovement to be global.
Here is an Example of setting a bool from another script that has been added to a gameobject to true:
public GameObject myobject;
void Start()
{
myobject.GetComponent<myscript>().mybool = true;
}
Here is a link that may help with that movement system of yours. Using input for every single command key will make your script inaccurate and long, you could do something like this:
Are you making a superhot clone
I liked that game
Good luck!
Thanks for the help guys!
so i know that you post this on 2014 but gives errors
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cardPlay : MonoBehaviour
{
public bool YouHaveCard = false;
//takeTheCard
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("cardFound!");
YouHaveCard = true;
}
// Update is called once per frame
void Update()
{
if (YouHaveCard == true)
{
Destroy(this.gameObject);
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class cardPlay : MonoBehaviour
{
public bool YouHaveCard = false;
//takeTheCard
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(“cardFound!”);
YouHaveCard = true;
}
// Update is called once per frame
void Update()
{
if (YouHaveCard == true)
{
Destroy(this.gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class doorSys : MonoBehaviour
{
public GameObject cardElson;
// Start is called before the first frame update
void Start()
{
cardElson.GetComponent<cardPlay>().YouHaveCard = false;
}
// Update is called once per frame
void Update()
{
if(YouHaveCard == true)
{
Destroy(this.gameObject);
}
}
}