Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Twun · Sep 16, 2014 at 03:38 PM · javascriptinstantiatearraywaypoint

How to use an array to set path by instantiating nodes?

I have a GameObject which for now is just a cube. What I am trying to do is to get the cube to follow a path that I create in real time by clicking in the game world.

I have an array set up which, once instantiated, each waypoint is added to but the cube will not register the added waypoints and instead gets stuck in a point in space which is unrelated to any of the instantiated waypoints.

I'm sure it's something very simple which I have overlooked. I have tried searching for similar problems but without much luck.

The code:

 #pragma strict
 
 import System.Collections.Generic;
 
 //var waypointList = new List.< Transform>();
 var waypoint : Transform[];
 var speed : float = 20;
 var currentWaypoint : int = 0;
 
 var insWP : GameObject;
 var wpCookie : GameObject;
 var ray : Ray;
 var hit : RaycastHit;
 var hoverWP : Vector3;
 
 var loop : boolean = true;
 
 
 function Update(){
 
 
 if (Input.GetButtonDown ("Fire1")) {
             // Construct a ray from the current mouse coordinates
             var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             
             if (Physics.Raycast (ray, hit)) 
                 {
                 
                 hoverWP = Vector3(hit.point.x, hit.point.y + 10, hit.point.z); 
             
                 Instantiate (insWP, hoverWP, transform.rotation);
                 Instantiate (wpCookie, hit.point, Quaternion.identity);
                 
                 
                 addToArray( insWP );
                 Debug.Log(waypoint.length);
                 
                 }
 }
 
     
     if(currentWaypoint < waypoint.length)
     {
 
     //var target : Vector3 = waypoint[currentWaypoint].position;
     
     var moveDirection : Vector3 = waypoint[currentWaypoint].transform.position - transform.position;
 
     var velocity = rigidbody.velocity;
 
         if(moveDirection.magnitude < 1)
         {
         currentWaypoint++;
         }
         
         else
         {
         velocity = moveDirection.normalized * speed;
         }
     
         
     rigidbody.velocity = velocity;
     
     
 
     }
     
     else
     {
         if(loop)
         { 
         currentWaypoint = 0;
         }
         else
         {
         velocity = Vector3.zero;
         }
     }
 
 }
 
 
 function addToArray ( obj : GameObject)
 
 {
  
     waypoint += [obj.transform];
  
 }
 
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
0
Best Answer

Answer by robertbu · Sep 27, 2014 at 01:45 PM

Your main problem is on line 31 and 35. You are adding the the prefab to your waypoint list. You want to add the game object you created. Change line 31 to:

 var obj : GameObject = Instantiate (insWP, hoverWP, transform.rotation);

...and then line 35 to:

 addToArray( obj );

This will get you past the issue in your question, but I suspect there are other issues you will need to work through.

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 Twun · Oct 11, 2014 at 04:56 PM 0
Share

Thank you. Sorry for the delayed response, I've been busy with other projects in the mean time. As you suggest I'll probably need to iron out subsequent issues but your answer certainly got me going in the right direction.

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

2 People are following this question.

avatar image avatar image

Related Questions

How can I instantiate a GameObject directly into an array? 3 Answers

Assigning Waypoints to an instantiated enemy 1 Answer

Instantiate object in for loop with array 2 Answers

Instantiating from an object that's in an array 1 Answer

Instantiate into array : Out of range? 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