Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jordan Miller · Apr 27, 2010 at 05:30 PM · prefabprefabsselectone

affecting only one Prefab in Code. (script)

in my game I have units you click on and tell what to do (RTS). so far I've coded it so that the unit becomes aware of when I click it, then it starts paying attention to the terrain script in order to know where to go.

it seems ridiculous to have my units looking at a different script, and if the units were unique I could just send the data straight to their script using "gameObject.Find("Unit").GetComponent("UnitsScript").someFunction(bla bla bla);" BUT the problem is all the units are prefabs that have been instantiated. so I can't do that right? becuase they're all named the same thing. but that's really what I'd like to do instead of having the units go looking for their directions when I click on them.

anyway, if that doesn't make sense here's my code, you will see nothing is being passed to my unit script, the unit script is looking to the terrain script to see if I have clicked on it. take a look if you want. or if you have a way to pass info to just one of my prefabs that would be awesome! thanks!

here's that part of my code:

function be () //called when I click on the unit. {

while (true) { if (lookingforwhattodo) { Debug.Log ("looking for what to do"); hereiam = GameObject.Find("Terrain"); if (hereiam.GetComponent("TerrainScript").getHasPackage()) //if i clicked on the terrain. to tell it where to go { //find out where we're supposed to go and what we're supposed to do foundwhattodo = hereiam.GetComponent("TerrainScript").VoodooYouDo(); foundwheretogo = hereiam.GetComponent("TerrainScript").getDestination(); Debug.Log ("found where to go" +foundwheretogo); Debug.Log ("found what to do" + foundwhattodo); //put the new location in my array list //p = 0; places.Push(foundwheretogo);//places.Add(foundwheretogo); doing.Push(foundwhattodo);

         //set back to null
         hereiam.GetComponent("TerrainScript").setHasPackage();

         if (hereiam.GetComponent("TerrainScript").deselect)
         {
             lookingforwhattodo = false;
         }
     }
 }

if (places.length >=1 && doing.length >= 1) { if (doing[p] == "Go") { ......

So thats the Code in my Unit. Here's the Code in my TerrainScript:

var haspackage : boolean = false; var voodo : String; var destination2 : Vector3; var deselect : boolean = true;

function OnMouseDown() { if (voodo != "null") // if I've clicked my menu to tell the unit what to do (go, attack, etc.) { deselect = false;// the unit is looking at this script presumably. var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit, 2500)) { destination2 = hit.point; haspackage = true; } }
else //if I'm just clicking on the terrain without having clicked on a menu Item first. { deselect = true; }

} function setVoodoo(thatudo:String) { voodo = thatudo; }

function VoodooYouDo() { return voodo; } function getDestination() { return destination2; } function getHasPackage() { return haspackage; } function setHasPackage() { haspackage = false; voodo = "null"; destination2 = Vector3(0,0,0); }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by StephanK · Apr 27, 2010 at 06:20 PM

I didn't really get what your code is doing and I'm not sure what you want to achieve, but if you want to call a function of a specific instance based on the name you could make sure that every instance of your prefabs gets a unique name in your scene. Something like:

GameObject go = GameObject.Instantiate("MyPrefab");
go.name = GenerateUniquename();

Much easier would be to keep your units in Lists or arrays, so they can be easily accessed by a specific index.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jordan Miller · Apr 27, 2010 at 07:22 PM 0
Share

dude, this is exactly what I need! thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Insert a prefab into scene in a script? 1 Answer

Spawn a prefab 1 Answer

Communicating between objects in the same prefab? 1 Answer

Random Script Failure 1 Answer

How to apply a texture to a prefab? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges