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
1
Question by ohkayem · Dec 16, 2017 at 04:09 PM · array of gameobjectsarraylist

Adding Waypoints using a List

Dear Coders I'm trying to add Waypoints to a dynamic list. So far, I've added GameObjectWithTag "Waypoint" to the list - but the code below only allows for a waypoint to be constantly added to a list.

I'm trying to randomly instantiate a waypoint as the game progresses and only add the waypoint once to the list but I really don't know where to start with this.

Code is below - any help would be gratefully appreciated!

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq;

 public class WaypointManager : MonoBehaviour {
 
     public Transform wayPointManager; 
 
     private GameObject wayPoint; 
 
     public List<GameObject> wayPoints = new List<GameObject>();
 
     void Start ()
     {
 
         wayPoint = GameObject.FindGameObjectWithTag("WayPoint");
 
         wayPoints.Add (wayPoint); 
     }
 
     void Update (){
 
         if (wayPoints == null || wayPoints.Count == 0)
         {
             return;
         }
             
         wayPoints.Add (wayPoint);
 
         wayPoint.transform.parent = wayPointManager;
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by karma0413 · Dec 16, 2017 at 04:42 PM

Does waypoint have additional information or scripts on the gameObject? Why use a gameObject to define a wayPoint instead of using like a Vector3 ( an x,y, z position ) ? I am just curious....

Comment
Add comment · Show 7 · 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 karma0413 · Dec 16, 2017 at 04:52 PM 0
Share

THIS, if Waypoints are empty... will actually INSTANTIATE a new gameObject in the scene and then add it to your list. Is that what you needed? Are you needing GameObject waypoint to be a physical gameObject in the scene?

     public Transform wayPoint$$anonymous$$anager;
     public GameObject wayPoint;
     public List<GameObject> wayPoints = new List<GameObject>();
 
     // Use this for initialization
     void Start () {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (wayPoints == null || wayPoints.Count == 0)
         {
             wayPoints.Add(GenerateARandomWayPoint());
             
         }
     }
 
 
     // --------------------------------------------Sub Routines--------------------------------------------
     // --------------------------------------------Sub Routines--------------------------------------------
     // --------------------------------------------Sub Routines--------------------------------------------
     // --------------------------------------------Sub Routines--------------------------------------------
     // --------------------------------------------Sub Routines--------------------------------------------
     // --------------------------------------------Sub Routines--------------------------------------------
 
     public GameObject GenerateARandomWayPoint()     // this will return back a gameObject of our choosing
     {
 
         bool setRelativeToWorld = true; // Set to TRUE if you want the object to spawn in the world coordinate system or set to FALSE if you wany the object to spawn in position relative to parent.
         GameObject clone = Instantiate(wayPoint, wayPoint$$anonymous$$anager, setRelativeToWorld) as GameObject;
         //clone.transform.position = ;
         //clone.transform.localPosition = ;
         
         return clone;
     }
avatar image karma0413 · Dec 16, 2017 at 04:59 PM 0
Share

You can Instantiate gameObjects using a variety of ways: (Taken from documentation)

 Instantiate(Object original);
 Instantiate(Object original, Transform parent);
 Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
 Instantiate(Object original, Vector3 position, Quaternion rotation);
 Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
 

avatar image ohkayem karma0413 · Dec 16, 2017 at 11:10 PM 0
Share

@karma0413

Thankyou very much for your help - this has worked perfectly. However, I'm trying to instantate the wayPoint at a random position and for some reason Random.Range is not working.

Would you be able to point in the right direction? Thankyou!

 public class Waypoint$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     public Transform wayPoint$$anonymous$$anager; 
     public GameObject wayPoint; 
     public List<GameObject> wayPoints = new List<GameObject>();
 
     public float timer;
 
     void Start ()
     {
 
         wayPoint = GameObject.FindGameObjectWithTag("WayPoint");
 
         wayPoints.Add (wayPoint); 
     }
 
     void Update (){
 
         timer += Time.deltaTime;
 
         if (timer > 10.0f) {
             GenerateARandomWayPoint (); 
             timer = 0f;
         }
             
         if (wayPoints == null || wayPoints.Count == 0)
         {
             wayPoints.Add (GenerateARandomWayPoint());
         }
 
     }
 
     public GameObject GenerateARandomWayPoint(){
     
         bool setRelativeToWorld = true; 
         GameObject clone = Instantiate (wayPoint,wayPoint$$anonymous$$anager,setRelativeToWorld) as GameObject;
         clone.transform.position = new Vector3 (10,1,10);
         return clone; 
     
     }
 }
     
avatar image karma0413 ohkayem · Dec 16, 2017 at 11:41 PM 0
Share

If my reply has helped you in some way, please give it a +1 or accept it at the correct answer. I am trying to build my reputation.

Let me pull my visual studio to make sure I give you the correct code... Okay, well first of all you could go about this in many ways, but I think an important consideration we must think about is
A) When we spawn an object, do we first want to make sure it does not collide with something? IE: Is there a wall right there? or another spawn point?
B) Who controls the spawning and how do we decide to do that?

For me in my program, it was a monster spawner so I had wanted to spawn in a radius and I also wanted to make sure another collision wasn't already occuring (IE: through the environment or because there's another monster already right there but has not yet moved out of the way)

There is also Y (height consideration) you need to think about when spawning items.

I also recognized that you set it's transform as a parents ( in the previous code ), but then you set it's WORLD position by utilizing the code: clone.transform.position = someVector3 , I just want to make sure you don't want to anchor it around the main transform parent in which case you would utilize the code: clone.transform.localPosition = someVector3. Also if it does not need a parent you can change the Instantiate code so your not settings its Transform as parent.

 clone.transform.position = RandomPosition()
 
 
 
 
 //later down in the code you can return that Vector3 with a function like so...
 
  public Vector3 RandomPosition()
     {
         Vector3 thisVector3 = new Vector3();
         thisVector3.x = UnityEngine.Random.Range($$anonymous$$FloatValue, maxFloatValue);
         thisVector3.y = UnityEngine.Random.Range($$anonymous$$Floatvalue, maxFlaotValue);
         thisVector3.z = UnityEngine.Random.Range($$anonymous$$floatValue, maxFloatValue);
         return thisVector3;
     }



Of course... You could simply set the range with code like this:

 thisVector3.x = UnityEngine.Random.Range(0f, 10f);
 thisVector3.y = UnityEngine.Random.Range(0f, 0f);
 thisVector3.z = UnityEngine.Random.Range(0f, 10f);
Show more comments
avatar image
0

Answer by ohkayem · Dec 16, 2017 at 04:48 PM

@karma0413 I hadn't thought of it like that although it could work. Once instantiated the waypoints need to remain in game as the enemies will continue to patrol randomly along them and so that's why I used gameobjects.

How would this work with vectors - wouldn't the problem still be the same with the way the script is?

Thanks!

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 karma0413 · Dec 16, 2017 at 04:55 PM 0
Share

I replied with a solution using gameObjects and instantiation.... Please see above...

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

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

List of bools assigned to each Gameobject in List/Array 0 Answers

Get all GameObjects by variable value 2 Answers

Calling Game object from Element list? 0 Answers

How to all GameObjects in array or 2 objects collide with 1 object at the same time 1 Answer

ArrayList Transform type! 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