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
1
Question by Meksho · Sep 11, 2014 at 10:28 PM · c#gameobjectarray

Alter a GameObjects(Clone) array from another script C#

I have a prefab Enemy2Way GameObject that I instantiate from my GameManager script. The Enemy2Way object has a script attached to it that controls waypoint movement. I control this via public static Transform[] patrolPoints array. Each point in the array should be a waypoint that the enemy patrols to. I am trying to instantiate empty gameobjects as the patrol points in my GameManager script during a level build function. I am able to instantiate the enemy object and all the waypoints, but I can't seem to figure out how to put those waypoints into that array so the enemy object moves to each one. If I drag the waypoint(clones) into the Enemy2Way(clone)s array after I push play it patrols just fine. How can I accomplish this via code?

Here is my code:

EnemyControl.cs

 using UnityEngine;
 using System.Collections;
 
 public class EnemyControl : MonoBehaviour {
 
     public static 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);
     }
 }

BuildLevel function in GameManager.cs

 public void BuildLevel(int whatLevel)
     {
         if (whatLevel == 0) 
         {
 
             Instantiate (Player, new Vector3 (0, 1, 7.5f), Quaternion.identity);
             Instantiate (Goal, new Vector3 (0, -1.7f, 0), Quaternion.identity);
             GameObject enemy1 = Instantiate (Enemy2Way, new Vector3 (0, 1, 0), Quaternion.identity) as GameObject;
             enemy1.GetComponent<EnemyControl>().patrolPoints[0] = Instantiate (patrolPoint, new Vector3 (0, 1, 0),Quaternion.identity) as Transform;
             enemy1.GetComponent<EnemyControl>().patrolPoints[1] = Instantiate (patrolPoint, new Vector3 (5, 1, 0),Quaternion.identity) as Transform;
 
             
         }
 
     }



Comment
Add comment · Show 3
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 contab009 · Sep 11, 2014 at 11:14 PM 1
Share

I would suggest converting the patrolPoints array from Transform to Vector3 type.

Anyway, the issue I notice for the moment is that you are not instantiating (with the new operator) the array objects. That would explain why it only works with manual assigning (when you actually pass the reference to an object). I am not sure if Transforms can be instantiated and this is why I suggested Vector3 objects, besides less memory usage - transforms also hold rotation and scale and they would be pointless if you're using only positions.

$$anonymous$$y suggestion is that, if you create the positions from the code anyway, you should declare a Vector3 array, then instantiate each object separatly with new. What you did had no success because you were only creating the array; you also need to create the objects, otherwise the array would be full of null references.

Also I apologize if I was too unclear. It is pretty late here and I'm going to sleep :) good luck on your project, I hope I helped so far.

avatar image Meksho · Sep 12, 2014 at 03:20 PM 0
Share

I will try the vector3 piece that you mentioned. However, I am able to instantiate the patrolpoint objects with the current code when I press play. Once play is pressed, I can drag the instantiated patrolpoint object into my patrolPoints array in the inspector and it all works the way I want. I just can't seem to figure out how to code the actual drag and drop process. If that makes any sense.

avatar image contab009 · Sep 13, 2014 at 11:01 AM 0
Share

I can't really see a bug in BuildLevel. Try instantiating as GameObject and then write

 [...].patrolPoints[i] = patrolPoint.GetComponent<Transform>();

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

C# SetActive GameObject Array 2 Answers

GameObjects static array NullReferenceException 1 Answer

How to modify specific GameObjects in an array of GameObjects? 1 Answer

C# Rotate GameObjects Regardless of List Size 2 Answers

C# GetComponent Issue 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