#Posting code

1 messages · Page 1 of 1 (latest)

knotty quail
#

hey, sorry, this is probably stupid, but how do I share the code from pastebin?

winged solstice
#

the link

orchid stratus
#

once you paste there should be some save or share button on most sites

sage sierra
#
private bool SpawnBall()
    {
        Instance = Instantiate(ballPrefab, m_launchEndPoint.position, Quaternion.identity);
        Instance.GetComponent<Rigidbody>().useGravity = false;
        Debug.Log($"SpawnBall: Instance: {Instance.name}");
        return true;
    }
#

this is what's called when you launch the ball. it doesn't use the ball you already created, just spawns a new one. you'll want to sub to its destroyed event, since it's a new object

knotty quail
#

The logic for validating that there is no instanced ball in the air is handled in OnPress() (although I really should probably just put it in SpawnBall()

orchid stratus
#

(post the paste site link already, its really annoying reading the original code in discord)

sage sierra
#

they editted their original post

orchid stratus
#

ah ok

knotty quail
orchid stratus
#

I would also not use unity event for this, it can just be a standard c# event since you're gonna need to subscribe at runtime anyways. but still you dont truly need events here unless. you're kinda doing both things where you use an event to know when the object is destroyed, AND checking the Instance anyways

knotty quail
#

I have it set up such that the prefab for the ball is connected to the prefab for the projectile instance handler

knotty quail
#

that's why instance is set to null in OnRelease

#

this code sucks major lol i'm sorry

orchid stratus
sage sierra
#

oh i see the first ball is used. it's like pre-loaded into the launcher or something. so a new one isn't spawned

orchid stratus
# knotty quail I really also should rename the `Instance` variable. That refers to the instance...

im aware, look at your code though. IsBallInAir is basically the same information to you as Instance.
these are all done at the same time

        Instance = null;
        IsBallInAir = false;
...
        IsBallInAir = true;
        Instance = null;
...
        if (Instance == null) 
        {
            Debug.LogError("OnPosition: Instance is null.");
            return;
        }
        if (IsBallInAir == true) 
        {
            Debug.LogError("OnPosition: Ball already in flight.");
            return;
        }
knotty quail
sage sierra
#

either way the fix is to sub to the destroyed event in SpawnBall()

knotty quail
sage sierra
#

either way the fix is to sub to the destroyed event in SpawnBall() lol

knotty quail
knotty quail
sage sierra
#

Im saying you should add this line to SpawnBall:

private bool SpawnBall()
    {
        Instance = Instantiate(ballPrefab, m_launchEndPoint.position, Quaternion.identity);
        Instance.destroyed.AddListener(OnBallDestroyed); <- add line
        Instance.GetComponent<Rigidbody>().useGravity = false;
        Debug.Log($"SpawnBall: Instance: {Instance.name}");
        return true;
    }
knotty quail
knotty quail
#

i'm really bad at using events lol

#

i'm kinda bad at coding, as this code demonstrates

sage sierra
#

idk what you're doing with the first one that makes it work so I think im missing information, but really it doesn't matter since it's working. the problem is the other balls after that, which subbing in SpawnBall would fix.

knotty quail
#

I'll attempt that and come back if need be

#

I think I'm gonna probably just redesign the whole system cause it sucks lol

sage sierra
orchid stratus
#

your entire ball code could literally be

public class Ball : MonoBehaviour
{
    public UnityEvent<Ball> destroyed = new UnityEvent<Ball>();
    
    void OnCollisionEnter(Collision other)
    {
        Destroy(gameObject);
    }
    void Update()
    {
        if (transform.position.y < -2)
        {
            Destroy(gameObject);
        }
    }
    void OnDestroy()
    {
        Debug.Log("Ball instance destroyed!");
        destroyed?.Invoke(this);
    }
}
#

although i would probably move the addForce method to there so the ProjectileInstanceHandler has less to do

knotty quail
knotty quail
#

it's handling inputs, the ball, the gamestate. It's basically a glorified game manager lmao

orchid stratus
#

when you instantiate an object, you can reference it by the component you want to actually spawn.

public Ball ballPrefab;
public Ball currentBall;

private bool SpawnBall()
{
    currentBall = Instantiate(ballPrefab, m_launchEndPoint.position, Quaternion.identity);
    currentBall.Setup(); // <- ball has a ref to its own rigidbody and sets gravity to false
}

private void LaunchBall()
{
    currentBall.Launch(m_direction * m_distance * FORCE_MULTIPLIER, ForceMode.VelocityChange);
    // ..then set current ball to false if you want
}
#

if you can simplify your own code it will be a lot easier for you to see whats wrong

orchid stratus
knotty quail
orchid stratus
#

unless you have parts where you actually need to control what is given input, its fine to just let this class subscribe to input

knotty quail
#

so just refactor it such that the functions responsible for manipulating anything about the ball is handled by the ball itself, then just call the relevant ball functions using the projectile instance handler?

#

that makes a lot more sense