December 13, 2019, 05:08:22 PM

Author Topic: Unity Monobehaviour class vs Normal non Monobehaviour class?  (Read 522 times)

Online Amon.

  • Full Member
  • ***
  • Posts: 150
  • What? There's no ceiling outside?
    • Amon.Pro
Unity Monobehaviour class vs Normal non Monobehaviour class?
« on: September 27, 2019, 12:21:01 AM »
I want to make a 3d rasterbar in Unity like I did in agk. Here is the agk code.
Code: [Select]
// Project: RasterBar3D
// Created: 2019-09-25

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "RasterBar3D" )
SetWindowSize( 768, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 768, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts


SetCameraPosition(1, -15, 0, -20)
SetCameraRotation(1, 0, 45, 0)
type Bar
    object as integer
    x as float
    y as float
    z as float
    angle as float
endtype
global BarList as Bar[]

TempZ = 0
TempAngle = 0
for xi = 0 to 10
    b as Bar
    b.object = CreateObjectBox(10, 1, 1)
    SetObjectColor(b.object, 255,0,0, 1)
    SetObjectCastShadow(b.object, 1)
    SetObjectReceiveShadow(b.object, 1)   
    b.x = 0
    b.y = 0
    b.z = TempZ
    b.angle = TempAngle
    TempZ = TempZ - 1.1
    TempAngle = TempAngle + 20.0
    BarList.insert(b)
next
   



do
    if BarList.length <> -1
        for xi = 0 to BarList.length
            SetObjectPosition(BarList[xi].object, BarList[xi].x, BarList[xi].y, BarList[xi].z)
            BarList[xi].y = BarList[xi].y + Cos(BarList[xi].angle) * 1 / 10.0
            BarList[xi].angle = BarList[xi].angle + 2.0
        next
    endif
    Print( ScreenFPS() )
    Sync()
loop
Now, in unity how would I do this? I understand I could have a normal c# class that does this but if it does not derive from monobehaviour then later on if I want to use the unity goodies like collision etc I won't be able to.
In a normal c# class I can use a constructor to create instances using 'new' much like in monkey-x but this can't be done if your class derives from a monobehaviour.
I havent used unity in any decent depth for a long while so I could do with some help.
Windows 10 Pro - 16GB DDR4 RAM - GEFORCE RTX 2080 TI 11GB - AMD RYZEN 7 3800X

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2223
Re: Unity Monobehaviour class vs Normal non Monobehaviour class?
« Reply #1 on: September 27, 2019, 12:36:23 AM »
The way I'd do it as a quick thing would be ( not the most optimised but it'd work - probably would need mono behaviour though ) :

1.. Create a cube the desired parameters you want.
2.. Give that cube a tag called "cube", add a box collider and then make that cube a prefab.
3.. Delete the cube from the scene.
4.. Instantiate as many cube prefabs as needed.
5.. Search for the tag and manipulate as required.
Until the next time...

Online Amon.

  • Full Member
  • ***
  • Posts: 150
  • What? There's no ceiling outside?
    • Amon.Pro
Re: Unity Monobehaviour class vs Normal non Monobehaviour class?
« Reply #2 on: September 27, 2019, 12:49:22 AM »
Yeah, I've done that already and it does work. I just wanted to use classes instead. I'm having problems in that if you have a unity class that derives from monobehaviour you can't use 'new' to create new instances like you can in a normal c# class that doesn't derive from monobehaviour.
I could instantiate objects but how do I access fields to assign values when instantiating?
Windows 10 Pro - 16GB DDR4 RAM - GEFORCE RTX 2080 TI 11GB - AMD RYZEN 7 3800X

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2223
Re: Unity Monobehaviour class vs Normal non Monobehaviour class?
« Reply #3 on: September 27, 2019, 01:50:58 AM »
I'm a Unity noob and my knowledge of it is super basic :P - But is this any help?
Until the next time...

Online Amon.

  • Full Member
  • ***
  • Posts: 150
  • What? There's no ceiling outside?
    • Amon.Pro
Re: Unity Monobehaviour class vs Normal non Monobehaviour class?
« Reply #4 on: September 27, 2019, 04:18:50 AM »
Yep, that linked helped heaps. Thank you.
Windows 10 Pro - 16GB DDR4 RAM - GEFORCE RTX 2080 TI 11GB - AMD RYZEN 7 3800X

Offline Xaron

  • Sr. Member
  • ****
  • Posts: 265
Re: Unity Monobehaviour class vs Normal non Monobehaviour class?
« Reply #5 on: September 27, 2019, 08:46:24 AM »
When using classes which derive from Monobehavior, you cannot use new to construct it.

Instead you're doing:
Code: [Select]
class MyClass : Monobehavior
{
  ...
}

...

// instantiation:
Gameobject cube = new Gameobject();
MyClass myclass = cube.AddComponent<MyClass>();