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 Meksho · Sep 02, 2014 at 05:47 PM · gameobjectinstantiatearray

How can I Instantiate a GameObject and then alter an array that is attached to that GameObject

I can currently Instantiate the GameObject just fine, but I have a script attached to it that has an array for waypoints that I would like the object to move back and forth to. In the editor I was able to manually set waypoints by dragging empty gameobjects into the array, but I would prefer to instantiate these empty objects and set their transform.position via this BuildLevel script instead. This is the part I am stuck on. The goal is to be able to have multiple enemies each with their own waypoints to patrol.

Here is the code I am using to instantiate my GameObject(some code is snipped):

 public GameObject Enemy2Way;

 public void BuildLevel(int whatLevel)
     {
         if (whatLevel == 0) 
         {
                     Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);
         }
     }


And here is the script that is attached to the Enemy2Way gameobject:

     public Transform[] patrolPoints;
     private int currentPoint;
     public float moveSpeed;
     
     void Start () {
         transform.position = patrolPoints [0].position;
         currentPoint = 0;
     }
     void Update () {
         if (transform.position == patrolPoints [currentPoint].position) 
         {
             currentPoint++;
         }
         if (currentPoint >= patrolPoints.Length) 
         {
             currentPoint = 0;
         }
         transform.position = Vector3.MoveTowards (transform.position, patrolPoints [currentPoint].position, moveSpeed * Time.deltaTime);
     }
 }



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
0

Answer by tanoshimi · Sep 02, 2014 at 05:49 PM

Change:

 Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);

To:

 GameObject Temp = Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity) as GameObject;
 Temp.GetComponent<WhateverTheNameOfYourSecondScriptIs>().patrolPoints = new Transform[...];

Replacing ... with your desired array of transforms.

Comment
Add comment · Show 6 · 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 Meksho · Sep 02, 2014 at 06:21 PM 0
Share

I get this message when trying your approach:

An object reference is required to access non-static member `EnemyControl.patrolPoints'

EnemyControl is the name of my second script in this case. I also tried setting the patrolPoints variable to:

public static Transform[] patrolPoints;

But it still prompted that error. For reference this is my updated code based on your example.

 public GameObject patrolPoint;
 
 GameObject enemy1 = Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);
 enemy1.GetComponent(EnemyControl.patrolPoints[0] = Instantiate (patrolPoint, new Vector3 (0, 1, 0), Quaternion.identity));

I put in the public GameObject as I was unsure as how to use Transform exactly as you stated in your example as it was different to what I was doing before.

avatar image Meksho · Sep 02, 2014 at 06:22 PM 0
Share

I seem to be unsure as to what goes into the Transform[...] What I was doing before doesn't seem to apply in this case and I seem to be stuck figure out what needs to be put in there. To clarify, what I did previously was to create an empty gameobject modify its transform via the GUI and it magically worked. Scripting those steps seem to be more complicated.

avatar image tanoshimi · Sep 02, 2014 at 06:28 PM 0
Share

Your syntax is wrong - take a look at my answer again. Your code should be:

 GameObject enemy1 = Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);
 enemy1.GetComponent().patrolPoints[0] = Instantiate (patrolPoint, new Vector3 (0, 1, 0), Quaternion.identity)).gameObject.transform;
avatar image tanoshimi · Sep 02, 2014 at 06:29 PM 0
Share

Ack - stupid UA won't let me post angled brackets in comments :(

avatar image Meksho · Sep 02, 2014 at 06:54 PM 0
Share

I looked up the GetComponent and I think I understand that part now. What I am using now is:

 enemy1.GetComponent<EnemyControl>().patrolPoints[0] = Instantiate (patrolPoint, new Vector3 (0, 1, 0), Quaternion.identity).gameObject.transform;

$$anonymous$$onodevelop doesn't seem to like the .gameObject.transform bit at the end for some reason.

Should I ins$$anonymous$$d instantiate a new gameobject first and then do: ~~~.patrolPoints[0] = newgameobject.gameobject.transform;

Show more comments
avatar image
0

Answer by Fohristiwhirl · Sep 02, 2014 at 07:00 PM

It might be better to create a method in the EnemyControl script that initialises the array. Using this approach you ensure that the array is handled the way that the EnemyControl script expects. You can do this by making the array private and adding a public initialization method. Then you would just use:

 GameObject go = Instantiate(Enemy2Way, new Vector3(0,1,5), Quaternion.identity);
 
 (EnemyControl)go.GetComponent(typeof(EnemyControl)).InitializeWaypoints(4); //4 is the number of waypoints to be initialized

Use of c# generic list might also be easier.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

instantiate gameobjects inside a canvas 0 Answers

How to put gameObjects to the list? 4 Answers

How do I instantiate same tag/name GameObjects with an array 1 Answer

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

Destroying 2D Array of Instantated Objects 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