How do I refrence a script?

I am making flappybird and I am going off of a Unity tutorial by Game Maker’s Toolkit. This script is meant for adding to the player score when the player collides with the collision shape in between the pipe prefab. I have been trying for a good few days but no matter what I try I cannot reference my script properly.


I have no idea what to do, I tried to use tags to reference the other script but I could not get that to work, Does anyone have any advice? My scoremanager(Empty actor) still has its ScoreM tag attached so I would still like to use a tag system but Unity has a much more straight forward method than Flax. Any advice will be appreciated.


This is my scoremanager script with my addscore function that I am trying to call in the OnTriggerEnter in my script above.

What tutorial are you following?

To reference other scripts on an Actor you use .GetScript() on the Actor objec

The problem i see right now is that the

Actor.HasTag() 

Is returning a boolean (true/false), so in turn the GetType<> you are using is trying to get the type from the Bool and not the Actor.

A quick fix without knowing what tutorial it is intending is this:

One thing before the code is to change

public Script scoremanager;

To this (It will reference your Scoremanager instead of flaxs’ base type Script)

public Scoremanager scoremanager;
if(Actor.HasTag("ScoreM")){
    scoremanager = Actor.GetScript<Scoremanager>();
}

This will check for the tag and then set your Script to the Score Manager.

Thanks!! That made things much better, I don’t get an error message anymore but now the only issue is finding my actor in my pipe prefab. Back in unity it was finding a script but flax needs an actor how am I supposed to do that in a prefab :thinking:? Here is the tutorial I followed https://www.youtube.com/watch?v=XtQMytORBmM&t=1988s he started at around 28:32 and here is my prefab

and middle pipe script:
using System;
using System.Collections.Generic;
using FlaxEngine;

namespace Game;

///


/// Middlepipes Script.
///

public class Middlepipes : Script
{
public Scoremanager scoremanager;

/// <inheritdoc/>
public override void OnStart()
{
    if (Actor.HasTag("ScoreM"))
    {
        scoremanager = Actor.GetScript<Scoremanager>();

    }

}
// Here you can add code that needs to be called when script is created, just before the first game update

/// <inheritdoc/>
public override void OnEnable()
{
    // Here you can add code that needs to be called when script is enabled (eg. register for events)
}

/// <inheritdoc/>
public override void OnDisable()
{
    // Here you can add code that needs to be called when script is disabled (eg. unregister from events)
}

/// <inheritdoc/>
public override void OnUpdate()
{
    // Here you can add code that needs to be called every frame


    

    void OnTriggerEnter(PhysicsColliderActor collider)
    {

        scoremanager.Addscore();
        Debug.Log("INCREASE");

    }

    

}

}

Sorry for copy pasting it the forums won’t let me put multiple elements. All of your help is greatly appreciated, thank you.

So… i will admit learning collision compared to unity is quite different.

In unity you just have like “OnTriggerEnter(var)” and it’s easy, one and done.

In flax, it’s more of like an event call than a method call where you have to setup more before hand.

so in flax, in your OnStart method you will need to get the collider. And then add the method, (delegate?, i’m not sure 100% what it’s called here) to it’s event call.

You can have it how you will, but I like to have the collider as a class level variable. So as you see, in the start method I set ‘c’ as the actor casted to the collider since the Actor ‘is’ of type collider as well.

And then also in the start i register a method to it’s TriggerEnter, where as in Unity it’s the OnTriggerEnter. Then in the new method you do what you want. It’s more setup that unity but it works the same.

Collider c;
public override void OnStart()
{
    c = Actor as Collider;
    c.TriggerEnter += middlePipeHit;
}

private void middlePipeHit(PhysicsColliderActor actor)
{
    throw new NotImplementedException();
}

Now, when the trigger enters (and you know it’s the bird), you can do whatever you want with the actor that entered (the bird). Or, if you intend to trigger the scoreUp, you can do that as well.

Now, i’m not entirely sure about what you mean by finding a script in unity. Are you talking about the GameObject.Find… etc?

Something similar >(That i haven’t used)< may be ‘Scene.FindScript()’

1 Like