- Home /
Structure Booling false when created and not enter C#
I wish I wouldn't have to make a bool from where I'm refering the bool in beginning
as if I make it in middle I can't have it true if mouse down as it'll make it false next frame
OR bool won't be assigned
and in structure it doesn't work too since I must not assign them in beginning as in code the commented one
and I don't want to change it to a Class
is there anykind of solution or I must necessarily instantiate it in beginning for a structure
using UnityEngine;
using System.Collections;
public struct ResizeWindow {
// public bool Resize = false;
public void ResizeF (ref bool Resize, ref Rect ResizingWindow){
if (Event.current.type.ToString() == "mouseDown"){
Resize = true;
}
if (Resize){
ResizingWindow = new Rect (0,0,Event.current.mousePosition.x + 1,Screen.height/3);
Debug.Log("sdpiorghn");
if (Event.current.type.ToString() == "mouseUp"){
Resize = false;
}
}
}
}
OR how would I create it false when I instantiate this structure?
ResizeWindow Resize.Resize = false;
or
ResizeWindow Resize = new ResizeWindow (Resize = false);
or something like that
so when it's created I automatically
if that's not possible just let me know
OR how would I do something that GUI.Dragwindow has? so I wouldn't need to instantiate stuff or ref or pass values
thanks in advance
Answer by Paulo-Henrique025 · Feb 15, 2013 at 02:16 PM
Try a constructor to set the bool when the structure is instantiated
using UnityEngine;
using System.Collections;
public class TestStruct : MonoBehaviour {
public struct MyStruct
{
public bool isAlive;
public MyStruct(bool setAlive)
{
isAlive = true;
}
}
// Use this for initialization
void Start ()
{
MyStruct ms = new MyStruct(true);
Debug.Log(ms.isAlive);
}
}
wow tried the code and did a few tweaks and it worked
I thought structures can't have a constructors
I was trying something like this before but it just didn't work
any way my working code now
using UnityEngine;
using System.Collections;
public struct $$anonymous$$yStructure {
public bool isAlive;
public $$anonymous$$yStructure(bool setAlive){
isAlive = true;
}
}
and testing - no errors
$$anonymous$$yStructure asdf = new $$anonymous$$yStructure(true);
tyvm :)
ahhhh now I get it structures can't have only constructors getting nothing
Structs share all the base concepts with Classes, take a look: http://stackoverflow.com/questions/4791883/c-struct-vs-class