-1

I have two scripts a parent and a child. The parent script provides the On Mouseover method for the child to use if Input.("Fire1") is used. and then it SHOULD disable the collider the mouse is over and score a point. But I am having an error where if I click on one prefab collider with the child script attached, it will then disable all prefab game objects. how can I fix this so my child script only allows for one disabling of a prefab at a time?

Parent Script

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

public class CollectableParent : MonoBehaviour
{
    
    protected GameManager gameManager;
    // Start is called before the first frame update
    void Awake()
    {
        gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnMouseOver()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            this.gameObject.SetActive(false);
            gameManager.UpdateScore(5);
        }
    }

 
    

    
    
}

ChildScript

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

public class LevelOneJack : CollectableParent
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetButtonDown("Fire1"))
        {
            OnMouseOver();
           
        }
        
    }
   


}
derHugo
  • 83,094
  • 9
  • 75
  • 115
ChipMunk
  • 1
  • 3
  • Please use the correct tags! Note that [`unityscript`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a JavaScript flavor like custom language used in early Unity versions and is long **deprecated** by now! – derHugo Jan 03 '22 at 13:42

1 Answers1

0

so i removed the the child class if statement from the child class "Update" method and am instead calling the OnMoseOver method, in a override method. which works well, the objects now only get disabled one at a time. I figure the problem is that I was disabling the parent script there for disabling all children scripts and objects too.

the only issue I have now is, when I hover the moues over an object, it automatically fires the UpdateScore method, even though there is an If statement checking if Input is true before it does the action. Its because i had the if statement syntax wrong

This is the updated child script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LevelOneJack : CollectableParent
 {
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
       
     }
 
         public override void OnMouseOver()
          {
         if(Input.GetButtonDown("Fire1"))
             base.OnMouseOver();
             gameManager.UpdateScore(5);
          }
 
      }
ChipMunk
  • 1
  • 3