Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bugs, Prefabs and Updating Oh My!!!

Discussion in 'Scripting' started by shawnpigott, Sep 20, 2007.

  1. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I am working on a game that moves into new territory for me. The game involves picking a bug (an insect) from three types (paper, rock and scissors) and then trying to gather food from a playing field without being eaten by the other bugs. Once you've been eaten you try to choose a new bug that can eat the bug that just ate you, and so on.

    The Problem 1 - Scoring

    The bugs are all prefabs so once the type has been picked it is instantiated into the game. I have a 'Score' script attached to the bug but when it is called (food is eaten) I am unable to send the information to the 'Score' object. I think this has something to do with breaking the connection in the script because one item is a 'prefab instantiated' and the other is a 'regular game object'. I got past this by turning the 'Score' object into a prefab. This didn't work at first because the score didn't update. It updated the prefab, not the instantiated version. I fixed this by destroying and recreating the object each time, not pretty, but it worked.

    The Problem 2 - Back to choosing.

    Once the bug is destroyed I want the game to go back into chose mode. The choose bug script turns off after the bug has been picked. This is the same problem as before but I can't figure a way around it yet. The 'instantiated prefab bug' needs to access the 'pick bug status' but the script won't allow me to choose the 'bug start' object to access the script.

    Basically I think it all boils down to sending information between instantiated objects, prefabs and singular game objects.

    Any help or guidance would be appreciated.
     
  2. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    You either want a script with some static variables or maybe even a more flexible option using an empty GameObject with a Singleton class attached.

    Check out this thread for more info:
    http://forum.unity3d.com/viewtopic.php?t=6836&highlight=singleton

    Basically, you'll be able to do things from any class like this:

    Score.instance.Add(500);

    Sounds good, right? :)
     
  3. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    The 'static var' solved the problem. I will look into the other option you suggested. It looks like it has the flexibility I need. Being self taught I somehow missed the static variable option.

    Thanks
     
  4. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I have another bug game question.

    Given that insects can attached themselves to non flat surfaces, is there a relatively easy way to have a character confirm their walk to a curving surface? For example an ant crawling around an arm?

    I'm guessing a raycast could work in someway if gravity was turned off in the
    collider.

    Thoughts?
     
  5. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    Yes, a raycast and then you can get the surface normal from the RaycastHit.