Welcome, Guest! Login | Register
script interaction not working?
Info
Report
194 Posts

i want to increase the value in a script by activating another script through trigger, but even if there's no mistake noted on the debugger the value stays the same aaaa

here's the script that is supposed to increase the value of the other script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;


public class POWERUPPOWER : MonoBehaviour
{
    public GameObject playa;
    Shooting punteggio;
    public GameObject pickupEffect;
    public float duration = 4f;
    public float multiplier = 1f;
    // Start is called before the first frame update

    void Start()
    {
        punteggio = playa.GetComponent<Shooting>();

    }

    void OnTriggerEnter(Collider other)
    {
        {
            if (other.CompareTag("Player"))
                StartCoroutine(Pickup(other));
            Shot();
        }


    }
    void Shot()
    {
        Shooting stats = playa.GetComponent<Shooting>();
        stats.bulletSpeed += multiplier;
    

    }
    // Update is called once per frame
    IEnumerator Pickup(Collider player)
    {
        Instantiate(pickupEffect, transform.position, transform.rotation);
        GetComponent<MeshRenderer>().enabled = false;
        GetComponent<Collider>().enabled = false;
        Destroy(gameObject);
        yield return new WaitForSeconds(duration);

    }
}

AND HERE'S THE REFERENCE SCRIPT

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting1 : MonoBehaviour
{

    public float bulletSpeed = 3000;
    public GameObject bullet;
    public float ShootRate = 1f;
    private float timestamp = 1f;


    AudioSource bulletAudio;

    // Use this for initialization
    void Start()
    {
        bulletAudio = GetComponent<AudioSource>();

    }

    void Fire()
    {
        //Shoot
        GameObject tempBullet = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
        Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody>();
        tempRigidBodyBullet.AddForce(tempRigidBodyBullet.transform.forward * bulletSpeed);
        Destroy(tempBullet, 0.5f);
        timestamp = Time.time + ShootRate;
        //Play Audio
        bulletAudio.Play();

    }
Created on: 1 week ago
Edited on: 1 week ago
Info
Report
271 Posts

It's probably because your object is being destroyed before it even has the chance to get to the Shot() method. Seems like your Pickup() coroutine is destroying the object. Aside though, you seem to disable the mesh renderer and collider, which is redundant as literally the next line you're destroying the object.

Try to start the Pickup() coroutine after you call Shot().
Created on: 1 week ago
Edited on: 1 week ago
Info
Report
194 Posts

Quote from: Power Supersport

It's probably because your object is being destroyed before it even has the chance to get to the Shot() method. Seems like your Pickup() coroutine is destroying the object. Aside though, you seem to disable the mesh renderer and collider, which is redundant as literally the next line you're destroying the object.

Try to start the Pickup() coroutine after you call Shot().

sadly that seems to do nothing, the value still stays the same :/
Created on: 1 week ago
Info
Report
271 Posts

Are you sure that the trigger works? Does the object get destroyed at all? And could you please tell me the variable you want to change as well?
Created on: 1 week ago
Info
Report
194 Posts

Quote from: Power Supersport

Are you sure that the trigger works? Does the object get destroyed at all? And could you please tell me the variable you want to change as well?

the object does indeed get destroyed, i want to change the public float bulletSpeed in the first script
Created on: 1 week ago
Info
Report
271 Posts

I can't figure it out in this state. There could be other code interfering.

A thing you can try though. Just put the Shot() method in the Pickup() coroutine before destroy. See if that works. If it doesn't, then your playa object might be wrong or something there is off. There is no way this value isn't changing.

Also, are you getting any errors? NullReferenceException specifically?
Created on: 1 week ago
Edited on: 1 week ago
Info
Report
194 Posts

Quote from: Power Supersport

I can't figure it out in this state. There could be other code interfering.

A thing you can try though. Just put the Shot() method in the Pickup() coroutine before destroy. See if that works. If it doesn't, then your playa object might be wrong or something there is off. There is no way this value isn't changing.

Also, are you getting any errors? NullReferenceException specifically?


nope, no errors, and even changing position of the Shot() method seems to do nothing
i checked and my playa object has the Shooting1 script in it just as intended
man i have no idea, there's no other script interacting with the playa object
Created on: 1 week ago
Info
Report
271 Posts

Is it possible, by the smallest smallest chance, that your multiplier value is set in the inspector to 0?
Created on: 1 week ago
Info
Report
194 Posts

Quote from: Power Supersport

Is it possible, by the smallest smallest chance, that your multiplier value is set in the inspector to 0?

even after setting it to the same number as the script nothing changes
Created on: 1 week ago
Info
Report
271 Posts

It doesn't make sense otherwise. There must be something else broken if you correctly tried all of my suggestions.
Created on: 1 week ago
Info
Report
194 Posts

Quote from: Power Supersport

It doesn't make sense otherwise. There must be something else broken if you correctly tried all of my suggestions.

ehhh i guess i'll figure it out eventually myself, i'd post screenshots but as of now its not possible
its definitely gonna be something incredibly stupid and minor that somehow escaped my eyes
Created on: 1 week ago
Reply
Preview
Post
Or use the advanced reply form here...
Sponsored