Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Ready to build, but cant sort this exception...

Discussion in 'Scripting' started by Retro420, Jun 4, 2017.

  1. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    Hello. I have the first level of my game done and ready to build, everything works great but I keep getting this exception:


    upload_2017-6-4_20-8-58.png

    I have a moving bridge in my level, and this exception doesnt stop anything from working properly. But every time I try to build project it wont let me because of this.

    Here is the code for my bridge:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BridgeMovement : MonoBehaviour {
    6.  
    7.     public Transform DestinationSpot;
    8.     public Transform OriginSpot;
    9.     public float Speed;
    10.     public bool Switch = false;
    11.  
    12.  
    13.     void FixedUpdate ()
    14.     {
    15.         if (transform.position == DestinationSpot.position)
    16.         {
    17.             Switch = true;
    18.         }
    19.  
    20.         if(transform.position == OriginSpot.position)
    21.         {
    22.             Switch = false;
    23.         }
    24.  
    25.  
    26.         if(Switch)
    27.         {
    28.             transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed);
    29.         }
    30.  
    31.         else
    32.         {
    33.             transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed);
    34.         }
    35.     }
    36. }
    37.  
    I am a beginner at coding, and the whole point of this project was as a learning environment for myself, but as far as I can tell there is nothing wrong with this code so I'm stumped. I dont know what is wrong with it.

    This is the inspector for my bridge object:
    upload_2017-6-4_20-12-46.png

    If anyone can help me out with some advice or further reading etc, I would be extremely grateful.

    Thanks.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Everything looks good, as your screenshot seems to show..
    However, check that there isn't another copy of this script in your game somewhere by accident (or perhaps on purpose, but that doesn't have that variable set in the inspector).
     
    Retro420 likes this.
  3. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    Hi. Thanks for the quick reply.
    I was sure the code was right, thanks for the confirmation. :)

    I have , I think, 4 moving bridges in the level. Only the first one has the Destination/OriginSpots set in the inspector because if i do all of them, none of the bridges move which is a bit confusing. Other than that the script isnt attached to anything else.
     
  4. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    A little tip on what Methos5k suggested...
    You can easily find where you may have accidentally attached a script without remembering/knowing by right clicking on the script itself in your project explorer and selecting "Find References in Scene". It will filter your heirarchy to show only things that have that script on them!
     
    Retro420 likes this.
  5. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    One other thing I've ran into before.. if you like to change datatypes of variables (ex. your "Destination Spot" wasn't a transform before, but a "GameObject"), sometimes that can cause nightmares. Change it to none, and drag it in again. In fact, if you are certain you have no other hidden usages of this script somewhere.. do that. Given that you're repeatedly getting the error, I suspect that's likely what is going on here.
     
    Retro420 likes this.
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool tip , as I was posting about finding references in the scene. I didn't even know about that lol.
    And the other one, too, in the next post.. I had heard about that but it doesn't hurt to check.

    So, as for your error, I can understand being flooded with errors, but it's weird that it won't build. You would think that would only matter when the code is running for that error.

    Do the other objects with this script ever move to destination spots at some point in the game? If not, they don't really need that same code..
     
    Retro420 likes this.
  7. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    This one is fresh in my mind because I just ran into it yesterday (and once before too). I was pulling my hair out, trying to figure out why it was erroring similarly to Retro420's situation (that's where I also discovered my "tip", as I figured there had to be a way to find references to the script). Anyways, I ended up just dumping the script off each object and re-attaching it, and re-assigning everything. I later realized it worked because, while Unity is great at allowing you to drag a game object onto a property with a certain component type and figuring out that you want that component used, it doesn't seem to be great at realizing you also want the reference to the component changed (ex. from "collider" to "transform" - just a completely random example) should you later change the data type of that property. That might be a bug in specific versions, or perhaps it's always been that way... not sure.
     
    Retro420 likes this.
  8. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    Thanks for the great tips cstooch, good stuff to know for a beginner.
    Methos5k, no other object has any destination spots, only the bridge.
    I think the problem might be that my dest/origin spots are seperate objects(mesh renderer off) but they have the bridge movement script attached to them as well as the bridges themselves having the script. Would this cause my problem?
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I want to add again that I'm not sure why that wouldn't let you build..

    However, it's just as important to point out what is the point of having a script on other objects that isn't being used? :)

    Why do the dest/origin objects need that script if they don't use it, I mean.
     
    Retro420 likes this.
  10. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    :(
    I feel stupid!

    I made the bridge and attached the script to it, then i copied the bridge object and re-positioned/re sized etc, turned off mesh renderer and then re named them and dragged to the transform part of the attached script. I have just removed the script form all dest/origin spots and Im not getting the error anymore...YAY!!

    Its so ridiculously simple though, suppose its part of the learning experience.
    At least I got some good info out of this thread though, thanks alot to both of you for the tips and help. I appreciate it.
     
    cstooch likes this.
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hey, cool cool .. good that you got it working. All part of the process.. I'm pretty sure it won't be the last bug you ever run into haha :)
    Enjoy your game.
     
    Retro420 likes this.
  12. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    Speaking of last bugs.....I'm now getting this:

    upload_2017-6-4_20-48-9.png

    Its never ending lol :(
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm 99.9% sure that you cannot use the UnityEditor in a build.
     
  14. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    Ok. The script in question is this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor.SceneManagement;
    5.  
    6. public class EndOfLevel : MonoBehaviour {
    7.  
    8.  
    9.  
    10.     void OnTriggerEnter(Collider other)
    11.  
    12.     {
    13.         if (other.tag == "Player")
    14.             EditorSceneManager.LoadScene("Level002");
    15.     }
    16.  
    17. }
    Didnt think that code would work without the " using UnityEditor.SceneManagement;".
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    try :
    Code (csharp):
    1.  
    2. using UnityEngine.SceneManagement;
    3. // and later
    4. SceneManager.LoadScene();
    5.  
    I didn't even know about the other one ;) But ya.. that should fix it.
     
    Retro420 likes this.
  16. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    I had'nt heard of this one lol.
    That has worked perfectly, thank you.
    Thought I was doing so well, learn't alot, and then got foiled by simple things at the final stretch. :)

    Thank you.
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    haha, no problem. Glad I could help :) Take it easy.
     
    TaleOf4Gamers and Retro420 like this.