Search Unity

Strange Object Behaviour...

Discussion in 'Scripting' started by g0tNoodles, Jul 25, 2011.

  1. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    'ello folks, I have just put together a few scripts which turn particles on and off when clicking on the object itself or another object. At them moment I have three planes with textures on to represent what I want to be highlighted with particles. I also have three different scripts, each essentially the same but slightly different.

    Here is one of the scripts contents (like I say, all are essentially the same):
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SelectRedTexture : MonoBehaviour {
    5.    
    6.     public bool isMouseOver = false;
    7.     public bool redPegSelected = false;
    8.    
    9.     public GameObject redTarget1;
    10.     public GameObject redTarget2;
    11.     public GameObject redTarget3;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.    
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         if(redPegSelected == false)
    21.         {
    22.             particleEmitter.enabled = false;
    23.             particleEmitter.ClearParticles();
    24.         }
    25.     }
    26.    
    27.     void OnMouseOver()
    28.     {
    29.         isMouseOver = true;
    30.         if(Input.GetButtonUp("Fire1")  isMouseOver == true)
    31.         {
    32. //          textureIsSelected = true;
    33.             redPegSelected = true;
    34.            
    35.             if(isMouseOver == true  redPegSelected == true)
    36.             {
    37.                 particleEmitter.enabled = true;
    38.                
    39.                 SelectYellowTexture syt = (SelectYellowTexture)redTarget1.GetComponent("SelectYellowTexture");
    40.                 syt.yellowPegSelected = false;
    41.                
    42. //              SelectGreenTexture sgt = (SelectGreenTexture)redTarget2.GetComponent("SelectGreenTexture");
    43. //              sgt.redPegSelected = false;
    44.                
    45.                 SelectBlueTexture sbt = (SelectBlueTexture)redTarget3.GetComponent("SelectBlueTexture");
    46.                 sbt.bluePegSelected = false;
    47.             }
    48.         }
    49.     }
    50.    
    51.     void OnMouseExit()
    52.     {
    53.         isMouseOver = false;
    54.     }
    55. }
    56.  
    So, that is for my red texture, I also have blue and yellow ones as well. The problem is that if I click from red > yellow > red > yellow > red; the third time I click the red object, it gets destroyed.... I have nothing in my code (as you can see) which points towards any form of deletion. I can go from red > blue, blue > red, blue > yellow or yellow > blue as many times as I like and it does not delete. I'm confused as to why this is happening..:confused:

    Any ideas would be most welcome,

    thanks!

    Edit: I just tried going from red > blue > red > blue > red and that also deletes the red object...
     
    Last edited: Jul 25, 2011
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The code confuses me a bit... you said you have 3 planes, but the red controller has 3 other objects listed.. Yellow, Green and Blue.

    To get around all of this and to make it less confusing. Consider changing the way that you handle each object. Making all the code the same would be a good thought. That still would not be a great answer.

    Scripts below will probably need to be debugged.

    Create a single object, call it PlaneController and attach this script to it.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlaneController : MonoBehaviour {
    6.     public ArrayList planes;
    7.    
    8.     void ActivatePlane (GameObject obj) {
    9.         for(int i=0; i<planes.Count; i++)
    10.         {
    11.             if(planes[i].particleEmitter  planes[i] != obj){
    12.                 planes[i].particleEmitter.enabled = false;
    13.                 planes[i].particleEmitter.ClearParticles();
    14.             }
    15.         }
    16.         if(obj.particleEmitter)
    17.             obj.particleEmitter.enabled=true;
    18.     }
    19.    
    20.     void AddPlane(GameObject obj){
    21.         if(planes.IndexOf(obj)==-1){
    22.             planes.Add(obj);
    23.         }
    24.     }
    25. }
    26.  
    Now, create as many particle systems that you want and attach this to each one of them
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SelectTexture : MonoBehaviour {
    6.     public bool selected=false;
    7.     public PlaneController planeController;
    8.    
    9.     void Start(){
    10.         if(planeController)
    11.             planeController.AddPlane(gameObject);
    12.     }
    13.    
    14.     void OnMouseDown(){
    15.         planeController.ActivatePlane(gameObject);
    16.     }
    17. }
    18.  
    Now, for each one of them, you will need to fulfill the planeController variable. This means that the PlaneController has to be in the scene and a PlaneController script attached to it.

    Now, the concept is that when you click on a plane... It activates the particle system on it, and disables the others.
     
  3. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Sorry about not saying but I was extremely when writing this >.<*

    We are meant to have 4 colours to choose from Red, Yellow, Green Blue. I only put three in for testing.

    Thanks for replying btw, I tried to follow your instructions and when I added the scripts I got this error....

    Not not sure what I need to change to get rid of these, any ideas?

    Thanks,

    Cas