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 Ramlethal · Jul 02, 2019 at 08:27 PM · scripting problemgameobjects

Can I make one script work independent on different Game Objects?

The question is quite clear itself, but I'll make it understandable for everyone.

I have one script for NPC movement, the scipt randomize the path which NPC follows.

If I give that script to 5 different NPCs, they start walking the same way, as they working with the same randomly chosen pattern. I can easily make 20 scripts and name them like NPCWalk1, NPCWalk2, NPCWalk3 etc., but that's not the way.

I just want to know if I can make different Game Objects using the same script independent, like they're new copies without making them in folder.

Any ideas?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NPC_WalkScript1 : MonoBehaviour {
 
     System.Random losuj; //random
 
     int WalkAni=0, IdleAni=0; 
     public GameObject kamera;
     private float gravity = 20f; 
     private float vertical_Velocity; 
     public float speed = 1f; 
     private CharacterController character_Controller; 
     public Animator ani;
     bool trybrozmowy = false;
     private Vector3 plus, bok; 
     Vector3 move_Direction; 
     int ObranyKierunek=0;
 
     bool spacerek = true;
     int odpoczynek = -1;  
     int chodzenie = -1; 
 
 
 
     // Use this for initialization
     void Start () {
         losuj = new System.Random();
 
         character_Controller = GetComponent<CharacterController>();
         odpoczynek = losuj.Next(300, 781);       
         chodzenie = losuj.Next(60, 240);
         ObranyKierunek= losuj.Next(1, 10);
         WalkAni= losuj.Next(1, 9);
         IdleAni = losuj.Next(1, 7);
 
 
         if (ObranyKierunek==5)
         {
             ObranyKierunek = losuj.Next(1, 5);
         }
     }
     
     // Update is called once per frame
     void Update () {
 
         if (trybrozmowy == false)
         {
             MoveTheNPC();
         }
         else
         {
             ani.Play("IDLE"+IdleAni);
         }
 
         transform.eulerAngles = new Vector3(0.0f, transform.eulerAngles.y, transform.eulerAngles.z);
     }
 
 
     void MoveTheNPC()
     {
 
         if (spacerek == true && chodzenie>0)
         {
             chodzenie--;
             //////////////////////////////////////////////////////////////////////////////////////////////////////////
             if (ObranyKierunek == 7 || ObranyKierunek == 8 || ObranyKierunek == 9)
             {
                 plus = new Vector3(kamera.transform.forward.x, 0.0f, kamera.transform.forward.z);
                 move_Direction += plus;
             }
             else if (ObranyKierunek == 1 || ObranyKierunek == 2 || ObranyKierunek == 3)
             {
                 plus = new Vector3(-kamera.transform.forward.x, 0.0f, -kamera.transform.forward.z);
                 move_Direction += plus;
             }
             else
             {
                 plus = new Vector3(0.0f, 0.0f, 0.0f);
             }
 
 
             if (ObranyKierunek == 1 || ObranyKierunek == 4 || ObranyKierunek == 7)
             {
                 bok = new Vector3(-kamera.transform.right.x, 0.0f, -kamera.transform.right.z);
                 move_Direction += bok;
                 if (ObranyKierunek == 1 || ObranyKierunek == 7)
                 {
                     move_Direction /= 1.5f;
                 }
             }
             else if (ObranyKierunek == 3 || ObranyKierunek == 6 || ObranyKierunek == 9)
             {
                 bok = new Vector3(kamera.transform.right.x, 0.0f, kamera.transform.right.z);
                 move_Direction += bok;
                 if (ObranyKierunek == 3 || ObranyKierunek == 9)
                 {
                     move_Direction /= 1.5f;
                 }
             }
             else
             {
                 bok = new Vector3(0.0f, 0.0f, 0.0f);
             }
 
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(move_Direction.normalized), 0.2f);
 
             ani.Play("WALK"+WalkAni);
 
             move_Direction *= speed * Time.deltaTime;
 
             ApplyGravity();
             character_Controller.Move(move_Direction);
 
             move_Direction = new Vector3(0.0f, 0.0f, 0.0f);
             /////////////////////////////////////////////////////////////////////////////////////////////////
         }
         else if(spacerek==true)
         {
             spacerek = false;
 
             WalkAni = losuj.Next(1, 9);
             IdleAni = losuj.Next(1, 7);
 
             ani.Play("IDLE"+IdleAni);
 
             chodzenie = losuj.Next(60, 240);
             ObranyKierunek = losuj.Next(1, 10);
             if (ObranyKierunek == 5)
             {
                 ObranyKierunek = losuj.Next(1, 5);
             }
         }
         else if(odpoczynek>0)
         {
             odpoczynek--;
         }
         else
         {
             spacerek = true;
             odpoczynek = losuj.Next(300, 781);
         }
 
 
     }
 
 
     void ApplyGravity()
     {
         vertical_Velocity -= gravity * Time.deltaTime;
 
         // jump
         //PlayerJump();
         move_Direction.y = vertical_Velocity * Time.deltaTime;
 
     } // apply gravity
 
 }
 

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
2
Best Answer

Answer by Bunny83 · Jul 02, 2019 at 09:01 PM

Well, the issue is that all your NPCs are created almost at the same time. You create a System.Random object for each of your instances. However since the Random object is initialized with the current time they most likely get all the same initial seed and therefore will draw the exact same sequence of numbers.


One solution, that @Kennai suggested, is to use Unity's global UnityEngine.Random class. Since all will share the same random number generator they won't draw the same numbers.


If for some reason you want to have a seperate System.Random instance for each enemy, you have to ensure they all have different seeds. There are many ways this can be done, but most revolve around a static state that will ensure everyone gets a different seed. The simplest case is just using a static int which you initialize with a fix value. Each time one of your NPC creates his Random instance you just increment that static field and use that value as seed. Of course since the int always starts with the same value you always get the same seeds. So the first NPC that is created always gets the same seed and will move the same way each time the game is restarted.


To solve that you can simply initialize the static int also with a random number. For this you could use Unity's Random class. Another solution is to use a static System.Random instance to draw a seed for each instance Random object. However this has the potential to draw the same seed twice or multiple times, though the chances are pretty low.

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
avatar image
2

Answer by Kennai · Jul 02, 2019 at 08:42 PM

Hi, @Ramlethal! Instead of System.Random, please use UnityEngine.Random
UnityEngine.Random

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

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

Why wont my character stop moving when it collides with the obstacles?,Why doesn't my movement script stop? 0 Answers

Split a sphere (ai) into smaller versions of itself on collision? 1 Answer

Attach the ObjectManipulator MRTK component in runtime to instantiated GameObject? 0 Answers

Bullets follow the ship in 2D Space Shooter game 2 Answers

How to operate two or many gameobjects present on Screen? 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