- Home /
When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this?
I am trying to make a script that can attach to multiple objects, and in that script there is a private string called sceneDestination. It is a serialized field so it can be changed from the sidebar when clicked on the game object. Inside the script, I issue a line that says print(sceneDestination) to debug, however, it prints the sceneDestination variable attached to all the objects using the script. So, say I have two game objects using the same script, and they each have their own set sceneDestination variable. When i run the program, there is a OnTriggerEnter2D function that prints the sceneDestination variable, but when the object enters the trigger it prints all the sceneDestination variables instead of just the one that hit the trigger. I checked that no triggers are touching either
What am I doing wrong? Any help would be greatly appreciated :)
Hmm...can you post the code? Is the variable static or something?
I can post the code it's just more of a last resort thing since it's well... long but no, the variables are set up as so:
[SerializeField]
private string iconName;
[SerializeField]
private Sprite iconSprite;
[SerializeField]
private string sceneDestination;
[SerializeField]
private bool inFolder;
and the code for printing looks like this:
// Update is called once per frame
void Update () {
float jump = Input.GetAxisRaw ("Jump");
//Check Double Click
if (!oneClick && (jump == 1 || Input.Get$$anonymous$$ouseButtonDown(0))) {
oneClick = true;
timeDoubleClick = Time.time;
} else if (oneClick && (jump == 1 || Input.Get$$anonymous$$ouseButtonDown(0))){
print (sceneDestination);
oneClick = false;
}
//Check Timer for Double Click
if (oneClick) {
if ((Time.time - timeDoubleClick) > doubleClickDelay) {
oneClick = false;
}
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Cursor") {
selected = true;
}
}
void OnTriggerExit2D(Collider2D other){
if (other.tag == "Cursor") {
selected = false;
}
}
Hmm...very...weird. Try using public variables ins$$anonymous$$d of private serializefields.
Answer by MGFlow58 · May 16, 2018 at 02:38 AM
Thanks to @Eno-Khaon , I have fixed the problem by simply making an OnTriggerStay2D function and putting part of what i had in Update in there. For some reason, Update calls all active objects, while OnTriggerStay2D only calls the object it was activated on. Here is the fixed code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Icon : MonoBehaviour {
[SerializeField]
private string iconName;
[SerializeField]
private Sprite iconSprite;
[SerializeField]
private string sceneDestination;
[SerializeField]
private bool inFolder;
private bool selected;
private bool oneClick = false;
private float timeDoubleClick;
private float doubleClickDelay = 1.5f;
private SpriteRenderer iconRender;
private float jump;
// Use this for initialization
void Start () {
iconRender = GetComponent<SpriteRenderer> ();
iconRender.sprite = iconSprite;
}
// Update is called once per frame
void Update () {
jump = Input.GetAxisRaw ("Jump");
//Check Timer for Double Click
if (oneClick) {
if ((Time.time - timeDoubleClick) > doubleClickDelay) {
oneClick = false;
}
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Cursor") {
selected = true;
}
}
void OnTriggerStay2D(Collider2D other){
if (other.tag == "Cursor") {
//Check Double Click
if (!oneClick && (jump == 1 || Input.GetMouseButtonDown (0))) {
oneClick = true;
timeDoubleClick = Time.time;
} else if (oneClick && (jump == 1 || Input.GetMouseButtonDown (0))) {
print (sceneDestination);
oneClick = false;
}
}
}
void OnTriggerExit2D(Collider2D other){
if (other.tag == "Cursor") {
selected = false;
}
}
void OnMouseOver(){
//print (sceneDestination);
}
}
Thank you very much to all those who tried to help me!
Answer by Eno-Khaon · May 16, 2018 at 12:04 AM
According to your script, you're not restricting your click to a single object in any way.
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
// Any mouse click made anywhere will trigger this at any time
}
}
To remedy this, you can generally either perform the click detection itself using a Raycast() or by using the OnMouseDown() function, which will trigger when the mouse is clicked on that object (thereby acting in place of Input.GetMouseButtonDown(0) itself).
If you need more functionality between left and right clicks, you may also consider using OnMouseOver() and then calling Input.GetMouseButtonDown(#) inside that function to limit it to the object you're currently hovering over instead.
Answer by plindsey · May 15, 2018 at 02:08 AM
Are the gameobjects/scripts instantiated? Maybe they share the same object in someway?
They aren't instantiated through any script no, to test this I dragged a random object out to the scene and attached the script there with another set sceneDestination variable, and when running the OnTriggerEnter2D event for another object it still printed the one for the new object as well. I am just lost on why it activates all the objects in the scene that use the script; I even tried using the "this" keyword but nothing helped