• 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 OgreLord · Aug 09, 2014 at 07:19 AM · c#prefabs

[solved]Need help instancing prefab objects

i have a little plane object that flies from city a to city b and back to a. i have it set up so a gui.window opens when you click the plane object and it should show the Starting City and the Destination City in the window, which this does work. but the problem is when i have more than one plane object even if they are going to and from diffrent cities the Gui.window will show all the origin cities and all the destination cities for all the plane objects. so i need to figure out how to make the plane objects independent of each other so they hold their own information that im working with.

the plane model object is a prefab with a plane mesh, the plane texture, with a cube collider mesh and a rigidbody as well as the script im posting here attached to it.

i create the plane object to the scene with:

GameObject mapplanemodel = Instantiate(Resources.Load("MapPlaneModel"))as GameObject;

then the RouteModel script takes over and starts the plane object flying from a-b-a-b and so forth.

this is the script for the planemodel.

 using UnityEngine;
 using System.Collections;
 
 public class RouteModel : MonoBehaviour 
 {
     GameObject aorigin;
     GameObject bdestination;
     GameObject route;
     Routes aroute;
     public bool flight;
     bool guiplaneroute;
     RaycastHit hit;
 
     // Use this for initialization
     void Start () 
     {
         route = GameObject.Find("Route");
         aroute = route.GetComponent<Routes>();
         aorigin = aroute.origin;
         bdestination = aroute.destination;
 
         flight = true;
         guiplaneroute = false;
 
     }
 
     void OnCollisionEnter(Collision other)
     {
         Debug.Log ("we hit outside city: " + gameObject.name);
         print("This collider collided with: " + other.contacts[0].otherCollider.name);
 
         if (other.contacts[0].otherCollider.name == aorigin.name) 
         {
             Debug.Log ("we hit city: " +  other.contacts[0].otherCollider.name);
             flight = true;
             
         } else
             flight = false;
 
     }
 
     // Update is called once per frame+
     void Update () 
     {
 
         float speed = 100;
         float step = speed * Time.deltaTime;
         if(flight == true)
         {
         transform.LookAt(bdestination.transform.position);
         transform.position = Vector3.MoveTowards(transform.position, bdestination.transform.position, step);
 
         }
         else if(flight == false)
         {
             transform.LookAt(aorigin.transform.position);
             transform.position = Vector3.MoveTowards(transform.position, aorigin.transform.position, step);
 
         }
 
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
         if (Input.GetMouseButtonDown (0)) 
             {
             if (Physics.Raycast (ray, out hit)  && hit.collider.tag == "MapPlaneModel") 
                 {
                 guiplaneroute = true;
                 }
             }
 
         if (Input.GetMouseButtonDown (1))
             {
             guiplaneroute = false;
         }
 
     }
 
     void OnGUI() 
     {
         if(guiplaneroute == true)
         {
             //0,0 is top left corner of box, 100,100 is the size of the box
             GUI.Box (new Rect (Screen.width / 3, Screen.height / 3, 200, 250), "Flight Route Info");
             GUI.Label (new Rect (360, 570, 65, 25 ), ""+ this.aorigin.name);
             GUI.Label (new Rect (420, 570, 40, 25 ), ""+ this.bdestination.name);
         }
         
     }
 }
 

Comment
Add comment · Show 2
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 OgreLord · Aug 10, 2014 at 12:31 PM 0
Share

so if i use this to test object then everything works great, it shows exactly what that object has/is doing.

 void On$$anonymous$$ouseEnter()
 {
 
     guiplaneroute = true;
 }
 void On$$anonymous$$ouseExit()
 {
     guiplaneroute = false;
 }
 

so my plane gameobject is saving the info i need it to save im just not calling the correct object with my raycast hit check. this is calling all of them. so how do i change this check to all of them. to only check the one i am clicking on?

 if (Input.Get$$anonymous$$ouseButtonDown (0)) 
             {
             if (Physics.Raycast (ray, out hit) && hit.collider.tag == "$$anonymous$$apPlane$$anonymous$$odel") //&& hit.collider.name == "$$anonymous$$apPlane$$anonymous$$odel"
                 {
 
 
                 guiplaneroute = true;
                 }
             }
 
 

 


avatar image OgreLord · Aug 11, 2014 at 05:12 AM 0
Share

ok i figured it out

just gave them unique names on creation so i can find them in script.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Created Animations only play half within Animator 0 Answers

C# Non-Static Member Rigidbody2D.MovePosition 1 Answer


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