Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Suppapa · Sep 05, 2015 at 07:47 AM · scripting problemphysicstransformscriptingbasicspostion

Trying to make an object "Flee" from the player but it won't work?

I tried this code out and it doesn't work, an error pops up saying "Object reference is not set to an instance of an object (target)" my code (C#):

 using UnityEngine;
 using System.Collections;

 public class Flee : MonoBehaviour {
    public int moveSpeed = 10;
    new Vector3 dir;
    GameObject target;
    void Start(){
    target = GameObject.FindGameObjectWithTag("Player");
    }
    void Update(){
        dir = transform.position - target.transform.position;
        transform.Translate(dir * moveSpeed * Time.deltaTime);
    }
 }
Comment
Add comment · Show 4
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 getyour411 · Sep 05, 2015 at 07:49 AM 0
Share

Option #1: Your player is not tagged "Player".

avatar image Scribe · Sep 05, 2015 at 08:57 AM 0
Share

you don't need a new on line 6:

 new Vector3 dir; ? -> Vector3 dir;

EDIT: Oh so I just learnt something, what I wrote is not your problem, likely getyour411 is correct.

new Vector3 dir; simply overwrites any variable called dir in the base class, which in this case doesn't exist, and so makes no difference, though I guess in that case convention would still suggest not writing new!

To help answer: Try adding a Debug.Log after you set target -

 void Start(){
     target = GameObject.FindGameObjectWithTag("Player");
     Debug.Log(target);
 }

it will likely return null!

avatar image Suppapa · Sep 05, 2015 at 09:01 PM 0
Share

$$anonymous$$y player is indeed tagged as Player so.....I really am not sure

avatar image getyour411 · Sep 05, 2015 at 10:00 PM 0
Share

Option #2: The error is not co$$anonymous$$g from this code; as you have it posted a simple copy/paste into my project works fine (albeit the flee is more like a take-off into space) but nonetheless it works without error.

3 Replies

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

Answer by Suppapa · Sep 07, 2015 at 03:47 AM

All I have to do is make the objects be prefabs and then instantiated for some reason

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
0

Answer by wibble82 · Sep 06, 2015 at 10:25 AM

Hi

It's a bit hard to diagnose from just that code, but if it was me the first thing I would try is moving your 'FindGameObjectWithTag' line to inside the update function, and then checking it is valid each frame

 void Update()
 {
     target = GameObject.FindGameObjectWithTag("Player");
     Debug.Log(target);
     if(target != null)
     {
         dir = transform.position - target.transform.position;
         transform.Translate(dir * moveSpeed * Time.deltaTime);
     }
 }

This is definitely not the best way to do things, as that search will be slow. However I suggest it to validate the fact that you are finding the correct game object. If you are indeed detecting it correctly in start, that would suggest the object you found was being destroyed. If so, you'll need to work out why, and either make it find the correct object, notify the script when the object changes, or just do the search every frame the dodgy way!

-Chris

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
0

Answer by ywj7931 · Sep 06, 2015 at 10:34 AM

are you sure you have set the tag?

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 ywj7931 · Sep 06, 2015 at 10:30 AM 0
Share

try to use GameObject.FindWithTag im not sure about findgameobjectwithtag

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Parent mesh problem 0 Answers

When I try to save the velocity of a rigidbody and use it later it doesn't work? 1 Answer

Rotation drift with transform.eulerAngles 0 Answers

My objects start flying after I Instantiate them 0 Answers

How can I slow the velocity of my player without it affecting fall speed? 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