Search Unity

Script not fetching value properly from other script

Discussion in 'Scripting' started by Barachiel, Oct 19, 2014.

  1. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Hello all.

    Today I have an issue wherein I have a small script that, when the object it's on has collided, it sets a bool to true. This works fine and I can see the bool become true upon collision, in the inspector.

    I have another script, not on the same object, that has a reference to it. I'm instantiating the object with the bool script as a variable, so that I can access the specific object I've spawned, then I get a reference to it and check if the bool is true. If it is, it takes an action, if it isn't, it performs a different action. Even if the bool is true, however, it always performs the other action. These are in Unityscript.
    Here's the relevant parts of the script:

    Code (JavaScript):
    1. function SpawnModule(pendingExit:ModuleConnector2)
    2. {
    3.         print("Begin");
    4.     var newModule                    =    Instantiate(newModulePrefab,this.transform.position-Vector3(0,30,0),Quaternion.identity);
    5.         print("Instantiate");
    6.         newModule.transform.parent    =    dungeonParent.transform;
    7.         print("Set parent");
    8.         newModuleExits                =    newModule.GetComponent(Module2).GetExits();
    9.         print("Get new exits");
    10.         exitToMatch                    =    GetRandom(newModuleExits);
    11.         print("Picked exit");
    12.      
    13.         MatchConnectors(pendingExit, exitToMatch);
    14.         print("Matched connectors");
    15.      
    16.         print("Start yield");
    17.         yield PlaceOrDestroy(newModule);
    18.         print("End yield");
    19.         print("Finish");
    20. }
    21.  
    22. function PlaceOrDestroy(module:GameObject):IEnumerator
    23. {
    24.     if(module.GetComponent(Module2).collided)
    25.     {
    26.         Destroy(module);
    27.         print("Collision Detected");
    28.         isThisModuleColliding = true;
    29.         moduleAttempts++;
    30.     }
    31.     else
    32.     {
    33.         print("Module Placement Confirmed");
    34.         modulePlacementConfirmed = true;
    35.     }
    36. }
    As always, help is much appreciated.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    On line 24, the module2 that you are getting is on the newly instanced GO, so will have default varialbes.
    (line 4 - newModule = isntantaiate... line 17 - PlaceOrDestroy(newModule))

    Not sure why you are yielding for it
     
  3. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    The MatchConnectors function moves the newly created GO to where I'm after, so I'm checking it's collision after it gets there. As for the yield, while I don't need it there yet, there will be stuff after it where I'll need to ensure that the collision check occurs before the rest of the function continues, so yielding the PlaceOrDestroy function serves that need.
     
  4. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    I don't think this part is really that relevant.
    the most important question is
    where is the var "collided" ?
    where did you set it up
    did you correctly toggle it in the collision.
    did you check that PlaceorDestory is actually being called after collision has been detected?
     
  5. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    The collided var is on the script (called Module2) attached to the module that's being spawned. It starts false and, upon colliding with something that has the appropriate tag via the OnTriggerEnter function it's set to true.
    This part definitely works.
    For the purposes of checking things out, it also prints that it has collided and includes it's name and the name of the object it has collided with (the name of the modules all have a number on the end which is incremented by one each time one gets spawned, to make it easier to find what I need).

    This is the relevant part of my generator script at this point:
    Code (JavaScript):
    1. function SpawnModule(pendingExit:ModuleConnector2)
    2. {
    3.         print("Begin"+" - "+modulesSpawned);
    4.     var newModule                    =    Instantiate(newModulePrefab,this.transform.position-Vector3(0,30,0),Quaternion.identity);
    5.         newModule.name                =    newModule.name + " - " + modulesSpawned;
    6.         newModule.transform.parent    =    dungeonParent.transform;
    7.         newModuleExits                =    newModule.GetComponent(Module2).GetExits();
    8.         exitToMatch                    =    GetRandom(newModuleExits);
    9.  
    10.         yield WaitForSeconds(1);
    11.        
    12.         MatchConnectors(pendingExit, exitToMatch);
    13.        
    14.         print("Start yield"+" - "+modulesSpawned);
    15.  
    16.         yield PlaceOrDestroy(newModule);
    17.         print("End yield"+" - "+modulesSpawned);
    18.         print("Finish"+" - "+modulesSpawned);
    19. }
    20.  
    21. function PlaceOrDestroy(module:GameObject):IEnumerator
    22. {
    23.     yield WaitForEndOfFrame;
    24.     if(module.GetComponent(Module2).collided)
    25.     {
    26.         Destroy(module);
    27.         print("Collision Detected"+" - "+modulesSpawned);
    28.         isThisModuleColliding = true;
    29.         moduleAttempts++;
    30.     }
    31.     else
    32.     {
    33.         print("Module Placement Confirmed"+" - "+modulesSpawned);
    34.         modulePlacementConfirmed = true;
    35.     }
    36. }
    The first time a collision occurs, it prints in this order (I've omitted the numbered part of their names for readability):
    • Begin
    • Start yield
    • corridor-turn-2(Clone) has collided with junction-f-3(Clone)
    • Collision Detected
    • End yield
    • Finish
    This first time, it works. The module is removed, the int value that represents the number of tries is incremented and it goes on to try again (the IsThisModuleColliding variable is just something I have exposed to the inspector for debugging and has no functional impact).

    After this first time however, even immediately while it is trying for a second attempt at placing a module, it prints in this order and, obviously, fails to delete it and try again:
    • Begin
    • Start yield
    • Module Placement Confirmed
    • End yield
    • Finish
    • corridor-turn-2(Clone) has collided with junction-f-3(Clone)
    With the yield WaitForEndOfFrame at the start of the PlaceOrDestroy function I had hoped this would allow the collision to be processed before the rest, and the first collision to occur suggests that this works. I just don't know why it doesn't after this.

    Being that this is a yield in a function being called from a function that also has yields, I'm wondering if this is the issue and how I would go about achieving what I need if it's something I can't do.
     
  6. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Been a bit busy, but spent some more time on this recently.
    I moved all the code I had in separate functions into the start function to be a part of everything else.
    I also changed the Module2 script attached to each room so that instead of setting a local variable to true when triggered it now sets a bool variable on the main generator script to true.

    This all works well enough so far, but now it no longer works even the first time, and just prints to the console as in the second example in my previous post.
    I have two bools in my generator script called modulePlacementConfirmed and isThisModuleColliding.
    In the Module2 script it sets the latter to true upon trigger enter, while the former is set to true if it isn't colliding (or triggering) and breaks out of the loop to continue on with the next room.

    When a collision happens, both turn true.
    I use 'yield WaitForFixedUpdate' before checking to allow time for the collision to register.
    Here's the current script piece:

    Code (JavaScript):
    1.  
    2. do
    3. {
    4.    isThisModuleColliding = false;
    5.  
    6.    var newModuleObject =   GetRandomWithTag(modules2, newTag);
    7.      newModulePrefab   =   newModuleObject;                 //This is to convert the object to a GameObject to access it's components.
    8.            
    9.      print("Begin"+" - "+modulesSpawned);
    10.    var newModule           =   Instantiate(newModulePrefab,this.transform.position-Vector3(0,30,0),Quaternion.identity);
    11.      newModule.name         =   newModule.name + " - " + modulesSpawned;
    12.      //print("Instantiate"+" - "+modulesSpawned);
    13.      newModule.transform.parent   =   dungeonParent.transform;
    14.      //print("Set parent"+" - "+modulesSpawned);
    15.      newModuleExits         =   newModule.GetComponent(Module2).GetExits();
    16.      //print("Get new exits"+" - "+modulesSpawned);
    17.      exitToMatch           =   GetRandom(newModuleExits);
    18.      //print("Picked exit"+" - "+modulesSpawned);
    19.      yield WaitForSeconds(0.5);
    20.  
    21.      MatchConnectors(pendingExit, exitToMatch);
    22.      //print("Matched connectors"+" - "+modulesSpawned);
    23.  
    24.      print("Start yield"+" - "+modulesSpawned);
    25.      yield WaitForFixedUpdate;
    26.  
    27.      if(isThisModuleColliding)
    28.      {
    29.        Destroy(newModule);
    30.        print("Collision Detected"+" - "+modulesSpawned);
    31.        //isThisModuleColliding = true;
    32.        moduleAttempts++;
    33.      }
    34.      else if (!isThisModuleColliding)
    35.      {
    36.        print("Module Placement Confirmed"+" - "+modulesSpawned);
    37.        modulePlacementConfirmed = true;
    38.      }
    39.  
    40.      print("End yield"+" - "+modulesSpawned);
    41.      print("Finish"+" - "+modulesSpawned);
    42.  
    43.      yield WaitForSeconds(1.5);
    44.  
    45. }while(modulePlacementConfirmed == false && moduleAttempts < 6);
    46.  

    I have a bunch of other yields in there just to slow things down a little so I can see what's going on.
    I'm pretty stumped on this one and am pretty much on the verge of giving up on it, so any guidance or anything you can see that looks out of place, I'd appreciate the help a great deal.
     
    Last edited: Oct 28, 2014