I have a class named TestMaze. I have another class named DisplayHome which has a method called gameOver():
public void gameOver()
{
Console.Write("GAME OVER!");
Console.Write("Play Again? Y/N");
if(char.ToLower(Convert.ToChar(Console.Read())=='y')
//Main()
else
Environment.Exit(1);
}
How can I call the Main method?
PS. they have the same namespace. I just need to know how can I call the Main method again.
You should have a Play() method inside your Main... and GameOver() should call Play() if user enters 'y'.
Refactor your code. Move whatever needs to be called into another function, and call it from both, main, and gameOver.
Assuming Main is a static class method (which I'd imagine it is) you can simply use MyClass.Main(/*relevant args*/) - beware of course that it's going to be a fresh instantiation, it won't share any non-static variable data.
A possibly better solution however would be to put all your code into a separate class which is invoked/instantiated from Main() - your program can then pass a boolean back to the actual executable Main which will be used to decide whether or not to exit or loop.
Related
Destroy() method is not accessible in static method
public static void Die()
{
Destroy(gameObject);
}
But Destroy() is only accessible if:
public void Die()
{
Destroy(gameObject);
}
You can't call a non static function from a static function but you can do the opposite.
I need to make it accessible on another scripts
Make the Die function to be a non static function. Let's say that this script is named OtherScript.
public void Die()
{
Destroy(gameObject);
}
Then from another script, you can access it by finding the GameObject the OtherScript script is attached to with the GameObject.Find function then use the GetComponent function to get the OtherScript reference from the GameObject:
OtherScript otherScript;
void Awake()
{
GameObject obj = GameObject.Find("NameOfGameObjectOtherScriptIsAttachedTo");
otherScript = obj.GetComponent<OtherScript>();
}
You can now call the Die function with otherScript.Die(). Note that you must replace "NameOfGameObjectOtherScriptIsAttachedTo" with the name of GameObject the OtherScript script is attached to.
From your comments it looks more like you actually want to do what Programmer's answer shows.
I'm just adding this because your title asks How to Destroy Object from static method in Unity C#
If you really need it to be static (e.g. in a static class) you could use it like this
using UnityEngine;
public static class SomeStaticClass
{
public static void Die(GameObject obj)
{
Object.Destroy(obj);
}
}
but to be honest this is needed in very few cases. It might be helpful e.g. in an Editor script where you don't have any Component executing your code.
cannot kill a single static object, it dosnt work that way. please refer to the answer here.
the following excerpt is from the above link, and should explain for you...
*I think perhaps you've misunderstood the 'static' keyword a little bit.
To clarify, a bit... Imagine you have a class called 'Vehicle'.
A none-static variable means 'every vehicle has its own copy of this variable'. We might say 'every instance of vehicle has its own copy of the variable.
A static variable means 'there is only 1 of this value shared by all vehicles'. Here we'd say 'all instances of vehicle share the variable.
Following on from that, functions are a little harder to picture, but they work in much the same way:
A none-static function operates on an instance of the vehicle. The result is that it can use the 'this' operator (it makes sense!) and access both none-static member variables of it's instance, and the shared static ones
A static function isn't tied to an individual instance of a vehicle, so the 'this' operator doesn't make any sense (what would 'this' be?). It still makes sense for it to be able to access static variables, but again none-static ones don't make any sense - who's version of the variable would it be referring to?
Your 'Die' function looks like it is designed to operate on a given instance of your enemy. i.e. you are expecting calling 'Die' to mean 'kill this please'. As a result it should not be static. You'll also need to access the 'gameObject' variable, not the 'GameObject' type.*
I'm making a little text game. The starting dialogue is in my main (static) method. From there, it sends you to other methods depending on your choices.
Now I think I need to have an instance of my class for this to work.
For example:
Program p = new Program();
if(stuff){
p.room1();
}
else{
p.room2();
}
Within those other methods there are global variables that will change.
So above the main method there is:
public bool hasItem = false;
So room1() would look like,
public void room1(){
if(stuff){
p.hasItem = true;
}
}
I know I'm screwing something up with the main method. Do I declare the instance "p" inside or outside of the main method? I've tried both but get errors both ways.
Edit: I ended up declaring a static "Program" outside of the main method to use elsewhere. Thanks for the help!
First off, you can either create a static Program outside of your main method, or declare a program inside your main method, depending on your architecture.
Second, you don't have to reference your instance from within your instance methods. Just use the field name. like so:
public void room1(){
if(stuff){
hasItem = true;
}
}
you can use this.hasItem if you want to be explicit about it.
Or better yet, make a brand new class to keep your state in. Having instance members in the class with the main method is awkward design.
I'm trying to call a method from a different class but with no succes.
I have a CheckBox checkBox1 in my program, and I have a button that when I click it I want to uncheck the checkBox1.
If I put I method in the same file like this everything works:
public void close()
{
checkBox1.IsChecked=false;
}
But if I create a separate class file (class Close), and put there (in the same namespace, and with "using" the required elements). There no way to make it work. I tried to instance the class as:
Close operation = new Close();
operation.close();
I also tried to put the close method as static, but I was impossible because in my real program I have lots of variables that gives me an error because they are "non static" (?).
I've noticed that a MessageBox.Show("Hello"); works if I call the method this way, but the checkBox1 still unchanged. What can I do?
A basic principle of Object Oriented Programming is Encapsulation. It means that a class knows and operates only on the internal member variables of the class.
In your case the class Close doesn't know anything of the control variable named checkBox1 and thus, the method close (as written above) cannot operate. It could not even be compiled!. Instead, when the method close is part of the Window class it works as expected because the control variable checkbox1 is a member variable of the window class
However, if I understand your intentions, I advise you to avoid to create separate classes to handle user interface operations. Let the code that works with the UI elements stay with the class where the elements are defined.
The CheckBox.IsChecked returns a value of type bool, if you are trying to actually change the value of the check box (i.e. checked or not-checked) you need to use a different property.
Try:
public void close()
{
checkBox1.Checked = true;
}
This is assuming that the Close class indeed has a CheckBox control inside of it... your question is rather vague, and frankly naming a method close is probably not the best practice as .Close() is a fairly common method on many classes in WinForms.
I have a form that has a button which creates a new object and calls it's start() method.
The program works fine, however, I now want to create a stop button. I obviously cannot call the object's stop() method as it is elsewhere, but, I just can't think of the correct way of changing my code.
As I write this, the best thing I can think of is to take the MyObject myo = new MyObject("test"); and place MyObject myo; at the top of the class, outside methods and then try to set it from within the class.
What would you do in this situation?
It's all dependent on scope.
If you want the form, at any time, to have visibility to that object, placing it as a private/protected member within the form's object is probably a good route. (make sure it's not null though.
class MyForm
{
private MyObject myobject;
private MyForm(){
// create the object
myobject = new MyObject;
}
private void Start_Click(){
myobject.start();
}
private void Stop_Click(){
myobject.stop();
}
}
If this object is constantly referenced, you could follow a singleton pattern.
If this is something you can re-create based on [an/the] argument(s) passed to the construct, you can re-create it every time it's needed.
That is is exactly what you are supposed to do. Its called creating a member variable.
But make sure you don't call myo.stop() when myo is null!
You are correct. You have to store the object reference some place so that you can call its "stop" method later.
Should I do this in a static Ctor? Else if the Create method is public static everyone could call it and recreate the List of controls. That is not wanted!
In my public Get I would just check if my list of objects is empty and create it there before return it back to the caller. Don't forget to lock this part of code, to insure that only the first call on your empty list does the create.
I don't know exactly what you intend to do (your question is pretty vague), but if you want to make sure your initialization is executed once and only once before any call to any member of your class, the static constructor is the place to go.