Hello, I would like to understand why the code below results to an error in line 21:
I thought using a public class would be a smart way to set a value and bang the script at the same time, but it’s not playing along.
using UnityEngine;
using System.Collections;
using System;
public class Test : MonoBehaviour {
int someValue;
public class myClass
{
private int number;
public int Number
{
get
{
return number;
}
set
{
Number = value;
parse(Number); // computer says no
}
}
}
private void parse(int number)
{
someValue = number;
}
}
Because at line 21 you call ‘parse’ on the class ‘Test’ from the class ‘myClass’.
Thing is, ‘parse’ is an instance method. This means you need an instance of ‘Test’ to call that method. You’re accessing it as if it’s a static method.
Note, just because a class is defined inside another class doesn’t mean that it is part of the object-identity for an instance of the class that wraps. They’re distinctly different classes.
The only truly useful purpose of a class defined in another class is to control access modifiers. Otherwise it’s superfluous, they’re just classes independent of the other.
If you want to use the parse() method, you need to put it inside your myClass class:
using UnityEngine;
using System.Collections;
using System;
public class Test : MonoBehaviour {
public int someValue;
public class myClass
{
private int number;
public int Number
{
get
{
return number;
}
set
{
Number = value;
parse(Number); // computer says no
}
}
private void parse(int number)
{
Test.someValue = number;
}
}
}
You’d also need to make your someValue field public so it can be accessed outside of its class.
@paratroid - still won’t work, since ‘someValue’ is still an instance member, not a static member.
Furthermore, it doesn’t necessarily have to be public, because ‘myClass’ is inside ‘Test’, so access modifier wise, it has permission to access any and all members of ‘Test’… given that it logically makes sense.
The entire issue is that they’re attempting to access a instance member as if it were static. This is the KEY issue.
This (the key learning it appears) I don’t really understand:
However, by making both the int and the method static, it does actually seem to work.
thanks!
using UnityEngine;
using System.Collections;
using System;
public class Test : MonoBehaviour
{
public static int SomeValue;
public class myClass
{
private int number;
public int Number
{
get
{
return number;
}
set
{
Test.SomeValue = value;
Test.Bang();
}
}
}
public static void Bang()
{
// do stuff with SomeValue
}
}
Click on an object, then send its data (array of floats) to a GUI window that displays those values. The objects are generated at runtime. They would all use the same script
Could also be done using events
public delegate void Process(float[ ] data);
public static event Process OnProcess;
etc…
but I’m not sure if this is a good approach banging the same event from different sources to one receiver. Should be the other way around right?
Again, looking for the best way to do this and trying to learn and understand.
Why should the object care if some GUI is displaying information about it? So why couple that dependency that the object should be aware of the GUI? Especially not embedding that object’s class inside the GUI’s class, that’s just… blargh.
Furthermore, what sort of GUI window? Do you mean like a unity editor window? Because that uses a polling system… what’s the point of an event/signaling approach if the data is going to be polled anyway?
Why not the GUI window check for clicks, and if what was clicked is something it’s interested in, then it starts polling that object every update.
The GUI window is just a set of input fields, that should also pass the data back to the selected object data when changed. (but that’s another story)
It would be great to have the GUI item check for clicks on certain objects. But can this be done without a line of code on the objects it listens to? Without sorcery? I have no idea how to make this happen (hence post 1 etc.)
Well, post 1 has nothing to do with tracking clicks or anything. Honestly it doesn’t look like production code of any sort… I assumed it was a trimmed down example of a static reference.
In the context of what you’re asking, the code in post 1 has NOTHING to do with a GUI window, something getting clicked, nothing.
As for a GUI window tracking clicks. You don’t need any code in the object that is getting clicked. You can just poll if the mouse button was pressed, and perform a Raycast to get anything under the mouse position.
And that’s if you wanted to use physics. You could get GameObjects under your mouse without physics as well, it’s just a little more complicated.
Again the problem in a nutshell: let’s say you have a lot of buttons for all buildings you can create (or whatever) and you want to send the buildingnumber to a gamemanager(or wherever). You can change the number by making it public static, but how do you make the gamemanager do something when the number change?
Buttons:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class Buttons : MonoBehaviour, IPointerDownHandler {
public int BuildingNumber;
public void OnPointerDown(PointerEventData eventData)
{
GameManager.current.setSelectedBuilding(BuildingNumber);
}
}
Manager
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public static GameManager current;
int selectedBuilding;
void Start ()
{
current = this;
}
public void setSelectedBuilding(int i)
{
selectedBuilding = i;
print(selectedBuilding);
}
}