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
0
Question by loizos-95b · Nov 25, 2017 at 02:16 AM · unity 5instantiategetcomponentarray of gameobjectsclones

How do I acces a specific GameObject within the terrain????

How do I acces a specific GameObject within the terrain????

I had developed a small terrain that spawning a number of enemys in random locations. However the clones that are spawned have Missing (GameObject) alt text


However my Enemy script its attached below. Note that the script its working fine when the enemy is in the Hierarchy as an 'Object' instead of a clone:

     public GameObject target;
     public Animator animator;
     public AudioSource attackAudio;
     
     // Use this for initialization
     void Start()
     {
         animator = GetComponent<Animator>(); //Default Animator IDLE
         attackAudio.Stop();
     }
 
     // Update is called once per frame
     void Update()
     {
         . . . 
     }



What should i set to my to my for loop to get a specific GameObject?? alt text


clones.png (413.9 kB)
spawn.png (397.9 kB)
Comment
Add comment · Show 5
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 Dread_Boy · Nov 25, 2017 at 02:36 AM 0
Share

I'm pretty sure you just never assign target. When you spawn new tiger, you should assign target in TigerAI or maybe find your target in Start() method of TigerAI and assign it.

avatar image loizos-95b · Nov 25, 2017 at 03:03 AM 0
Share

Isn't that the way that I assign a new target?? (Spawn_Object_Script : Line 43) or do you suggest to move that code, or the one that has to be done, to the TigerAI??

avatar image Dread_Boy · Nov 26, 2017 at 08:59 AM 0
Share

What you do in line 43 is just changing tag of target object. I think at that point target is null so you also get Exception. But even if you didn't get an exception, you would only change tag of target which is not the same as assigning target. What is your target? Do you have reference to it somewhere? If it exists in scene when tiger is created, you can find it with FindObjectOfType<PlayerAI>(). Or if you have reference to it in spawning script, you can directly assign it with GetComponent<TigerAI>().target = player. player is reference to target, of course.

avatar image loizos-95b Dread_Boy · Nov 26, 2017 at 06:33 PM 0
Share

I used this and its working fine i think

public GameObject CloneTarget; //Attach a new prefab (same player prefab) in the

. . . .

 for (int i = 0; i < TigerGroup.Length; i++) {
 
                 Vector3 position = new Vector3(Random.Range(300, 600), 150, Random.Range(235, 710));
                 TigerGroup[i] = Instantiate(Original_tigerPrefab, position, Quaternion.identity) as GameObject;
 
                 TigerGroup[i].GetComponent<TigerAI>().target = CloneTarget; 
             }


Let me know if there is more efficient way that this. Thanks in advance.

avatar image sethuraj · Nov 27, 2017 at 09:51 AM 0
Share

Prefabs are designed to be scene independent. Prefab object scripts should not have reference to scene objects or components since the link will be broken, but can have reference to its own child object or components. Try getting reference to the target (assu$$anonymous$$g its the player) dynamically using the script. For instance, in the enemy script just near to getting the animator component.

 // Use this for initialization
     void Start()
     {
         //Get reference to the player object by name
         target = GameObject.Find("Player");
 
 
         //CONTINUE CODE
         animator = GetComponent<Animator>(); //Default Animator IDLE        
         attackAudio.Stop();
     }

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

160 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 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 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

How do I acces a specific GameObject within the terrain???? 1 Answer

Array values changing after gameObject is instantiated from prefab 1 Answer

How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer

Array with several gameobjects and their respective script 0 Answers

For loop and instantiate question 0 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