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 /
  • Help Room /
avatar image
0
Question by Mpsteve137 · Nov 07, 2017 at 06:06 PM · timecomponentnpcinstances

What is the proper way for making multiple instances of the same script?

So here is my situation: I am making a game where time is a factor and NPCs will move about there day based on the time. So instead of every NPC having an update function to check the time, I would like the script that calculates the time to call a function inside my NPC script every time the hour changes so that they can move to their next location. So my question is, how can I make an instance of each NPCAnimate script that is attached to each NPC, so that I can call a function from within their script? Here is what I have so far but it isn't working:

public GameObject[] npc; public NPCAnimate[] npca;

 void Start () {
     npc = GameObject.FindGameObjectsWithTag ("NPC");
     for (int i = 0; i < npc.Length; i++) { 
         npca [i] = npc [i].GetComponent <NPCAnimate>();
     }
 }

pls halp

Comment
Add comment · Show 2
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 Penca53 · Nov 07, 2017 at 06:45 PM 0
Share

So you want a single script that calculates the time and then that calls a funcion in every NPC script?

avatar image Mpsteve137 · Nov 07, 2017 at 06:52 PM 0
Share

Yes, I already have the time script sorted. Just need help figuring out how to reference each npc script

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Penca53 · Nov 07, 2017 at 07:17 PM

You can create a List and then put all your script in it:

public class callExample : MonoBehaviour {

 public GameObject[] gameObjectexample;
 public List<example> exampleScriptList;

 void Start () {
         
     gameObjectexample = GameObject.FindGameObjectsWithTag ("Example");
         
     for (int i = 0; i < gameObjectexample.Length; i++) { 
         exampleScriptList.Add(gameObjectexample [i].GetComponent <example>());
     }
 }

}

Then if you want to call a funcion in every list member:

     for (int j = 0; j < gameObjectexample.Length; j++) {

         exampleScriptList [j].exampleFuncion ();
     }





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 Mpsteve137 · Nov 07, 2017 at 07:19 PM 0
Share

perfect, thank you

avatar image Penca53 Mpsteve137 · Nov 07, 2017 at 07:20 PM 0
Share

sorry if "void Start () {" isn't in the right place... let me know if it works :)

avatar image Bunny83 Penca53 · Nov 07, 2017 at 07:24 PM 0
Share

You know that you can edit your answer?

Show more comments
avatar image Bunny83 · Nov 07, 2017 at 07:23 PM 0
Share

That is actually a bad example. Since you don't interact with the array in the inspector the variables shouldn't be public. You also don't initialize the "exampleScriptList" variable. Currently you rely on the initialization through the inspector. There the user could add elements before your code runs. Also if the script would be attached dynamically the list wouldn't be initialized at all.

Finally you did not properly mark your code as code and therefore the generic parameter of the List is not visible.

avatar image Penca53 Bunny83 · Nov 07, 2017 at 07:35 PM 0
Share

I used public variables because he used public variables. I could use a [SerializeField] private variable to create a private variable but visible in the inspector for debug.

avatar image
0

Answer by Bunny83 · Nov 07, 2017 at 07:19 PM

it isn't working

That's not a well description of your problem. Do you get an error? That means either your compiler or your runtime is already telling you what is wrong but you ask a question here and let us analyze your script from scratch....


What we can see is that you do not initialize your "npca" array. It should have the same length as your npc array.

     npc = GameObject.FindGameObjectsWithTag ("NPC");
     npca = new NPCAnimate[npc.Length];
     // [ ... ]
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

73 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

Related Questions

Can't drag and drop audio source 1 Answer

Why isnt the enabled property part of an interface? 0 Answers

Help Creating Explosion Knockback 0 Answers

Composite Pattern in Pure ECS,Composite Pattern in Unity ECS 0 Answers

Can't find 2d collider in components on Unity 2020.1.14f1 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