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 BertMDTV · Feb 11, 2015 at 12:53 PM · javascriptspawnsendmessagefollow playerfindwithtag

Setting a prefab clone as a variable in code

So in my game I only have one gameobject and that is a spawner. What is does is spawn an arena, player, and defensemen. So what is supposed to happen is the defensmen are supposed to follow the player when the game starts. The problem I am having is when everything spawns I can't get the player prefab clone to become the target for the defenseman.

Here is the code for the Player:

 var speed : int = 1; // Adjust to make your NPC move however fast you want.
 
 function Start () {
 
     GameObject.FindGameObjectsWithTag("Defenseman").SendMessage("SetPlayer");
 
 }
  
  function Update ()
  {
    transform.Translate(Vector3.forward * speed * Time.deltaTime);
  }

Here is the code for the Defensemen:

 #pragma strict
 var target: Transform; // drag the target here
 var speed: float = 5.0; // object speed
 private var moving = false; // object initially stopped
 private var dir: Vector3;
 private var plChar : GameObject;
      
      
 function Start () {
 }
      
 function SetPlayer () {
      
     target = GameObject.FindWithTag("Player").transform;
      
 }
      
 function Update () {
     dir = target.position - transform.position; // calculate the target direction...
     moving = true; // and enable movement
       
     if (moving){ // if movement enabled...
          // move the object in the calculated direction (world coordinates):
          transform.Translate(dir * speed * Time.deltaTime, Space.World);
        }
      
      
      
  }


So what I thought is when the player spawns he could use .SendMessage to the defensmen to use the SetPlayer function. In the SetPlayer function in the Defensmen script it basically says find anyone with the tag Player and make that the target. Well this doesn't work. When I start the game the defensemen just stay still and I get a few errors. They are either "UnityEngine.GameObject[].SendMessage" or "Object Reference Not set to an Instance of the Object". If anyone could help me find a way to set the player that spawns in the target for the Defesnmen that woudl be great.

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 chariot · Feb 11, 2015 at 12:58 PM 0
Share

Ehm... why target = GameObject.FindWithTag("Player").transform; ?

 #pragma strict
  var target: GameObject; // drag the target here
  var speed: float = 5.0; // object speed
  private var moving = false; // object initially stopped
  private var dir: Vector3;
  private var plChar : GameObject;
       
       
  function Start () {
  }
       
  function SetPlayer () {
       
      target = GameObject.FindGameObjectWithTag("Player");
       
  }
       
  function Update () {
      dir = target.transform.position - transform.position; // calculate the target direction...
      moving = true; // and enable movement
        
      if (moving){ // if movement enabled...
           // move the object in the calculated direction (world coordinates):
           transform.Translate(dir * speed * Time.deltaTime, Space.World);
         }
       
       
       
   }
avatar image chariot · Feb 11, 2015 at 12:59 PM 0
Share

And your moving script... dont know, but Im think it isnt perfect way to move character. But, who knows?

avatar image BertMDTV · Feb 11, 2015 at 02:22 PM 0
Share

So I changed the two scripts.

Player Script:

 var speed : int = 1; // Adjust to make your NPC move however fast you want.
  
  private var defense$$anonymous$$ans : GameObject; 
  
   function Start () {
   
      defense$$anonymous$$ans = GameObject.FindGameObjectsWithTag("Defenseman");
      for (var defense$$anonymous$$an : GameObject in defense$$anonymous$$ans){
         var def$$anonymous$$anScr : FollowPlayer = defense$$anonymous$$an.GetComponent(FollowPlayer);
         def$$anonymous$$anScr.working = true;
      }
   }
    
    function Update ()
    {
      transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }

and the Defensmen Script:

 #pragma strict
    var target: GameObject; // drag the target here
    var speed: float = 5.0; // object speed
    private var moving = false; // object initially stopped
    private var dir: Vector3;
    private var plChar : GameObject;
    public var working : boolean;
         
         
    function Start () {
    }
         
    function SetPlayer () {
         
        target = GameObject.FindGameObjectWithTag("Player");
         
    }
         
    function Update () {
        if (working == true){
           SetPlayer();
        }
        dir = target.transform.position - transform.position; // calculate the target direction...
        moving = true; // and enable movement
          
        if (moving){ // if movement enabled...
             // move the object in the calculated direction (world coordinates):
             transform.Translate(dir * speed * Time.deltaTime, Space.World);
           }
         
         
         
     }

I did have to change a few things like the active variable because I kept getting an error that it wasn't correct or something so I just changed it to working and I didn't get the error but there is a different problem. I get this error Assets/Scripts/Player$$anonymous$$oveForward.js(7,53): BCE0022: Cannot convert 'UnityEngine.GameObject[]' to 'UnityEngine.GameObject'. I don't know why and I don't know how to fix this problem but can you help me out with it. Even when I tried to fix a couple of things to get the game testing the defensmen still dont follow the player. Anyone have any ideas???

avatar image chariot · Feb 11, 2015 at 02:24 PM 0
Share

$$anonymous$$y mistake :)

  private var defense$$anonymous$$ans : GameObject[]; 
avatar image BertMDTV · Feb 12, 2015 at 01:20 AM 0
Share

So this seems to be working for me but it isn't actually follow my player its following like an area where the player spawns. I don't know how to get it to follow the player that actually in the game. Any idea's ???

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by chariot · Feb 11, 2015 at 01:08 PM

First script reworked

 var speed : int = 1; // Adjust to make your NPC move however fast you want.
 
 private vat defenseMans : GameObject; 
 
  function Start () {
  
     defenseMans = GameObject.FindGameObjectsWithTag("Defenseman");
     for (var defenseMan : GameObject in defenseMans){
        var defManScr : DefenseManScriptName = defenseMan.GetComponent(DefenseManScriptName);
        defManScr.active = true;
     }
  }
   
   function Update ()
   {
     transform.Translate(Vector3.forward * speed * Time.deltaTime);
   }

Second script:

  #pragma strict
   var target: GameObject; // drag the target here
   var speed: float = 5.0; // object speed
   private var moving = false; // object initially stopped
   private var dir: Vector3;
   private var plChar : GameObject;
   public var active : boolean;
        
        
   function Start () {
   }
        
   function SetPlayer () {
        
       target = GameObject.FindGameObjectWithTag("Player");
        
   }
        
   function Update () {
       if (active){
          SetPlayer();
       }
       dir = target.transform.position - transform.position; // calculate the target direction...
       moving = true; // and enable movement
         
       if (moving){ // if movement enabled...
            // move the object in the calculated direction (world coordinates):
            transform.Translate(dir * speed * Time.deltaTime, Space.World);
          }
        
        
        
    }
Comment
Add comment · Show 1 · 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 chariot · Feb 11, 2015 at 01:10 PM 0
Share

if u want to turn off finding player position after first use, just turn off it in your function

 function SetPlayer () {
         
     target = GameObject.FindGameObjectWithTag("Player");
     active = false;
 }
avatar image
0

Answer by jenci1990 · Feb 11, 2015 at 03:15 PM

The problem is in player script: FindGameObject and FindGameObjects are not same.

GameObject.FindGameObjectsWithTag("Defenseman"); //This return array of GameObject.

you have to do this:

 var listOfDefenseman : GameObject[] = GameObject.FindGameObjectsWithTag("Defenseman");
 for (defman : GameObject in listOfDefenseman) {
     defman.SendMessage("SetPlayer");
 }
 


If you have one defenseman script use this:

GameObject.FindGameObjectWithTag("Defenseman").SendMessage("SetPlayer");

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

When GameObject is spawned, rotate x 90. 1 Answer

instantiate a set amount 3 Answers

Enemy spawn issues 1 Answer

Unity WebGL UnityInstance.SendMessage Error: SendMessage: object Object1 does not have receiver for function moveObject 1 Answer

Trying to write enemy spawn in JS. 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