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
-1
Question by Given · Jan 26, 2011 at 01:54 AM · instantiateplayerfunctionbroadcastmessageloader

Backround Loader

Hi, I wrote a java script where if the player gets a certain from an object, that object changes into something else (a lowpoly version or even an empty object). This worked just fine, so I wrote the second part: changing it back when the player gets within a range. Not so fine. I've racked my brain for a possible problem, so I decided to plea for more experienced help. I get no errors at all; it just plain doesn't work.

To try it out your self, make 2 prefabs, one with the backroundloader.js attached and the other with the backroundreloader.js attached. Next assign the target of both to be your player (I use the FPS controller), and tag your player "Player". Lastly assign the object and oldobject to vars to their counterparts, and set a sightdistance. It SHOULD work.... but it just changes to the new object and never changes back, no matter how close I get. ANY help would be appreciated, and if get it working, you can use the scripts in any of your projects Just be sure to let me know how you did it.

Here are the scripts:

backroundloader.js (works fine)

var object : Transform; var sightRange = 30.0; var target : Transform; var changeDelay = 0.0;

function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; } // If the distance is greater than sightRange if (Vector3.Distance(transform.position, target.position) > sightRange) { Invoke("loadNew", changeDelay); }

function loadNew () { BroadcastMessage ("outofSight"); }

function outofSight () { // Destroy object Destroy(gameObject);

 // change to new object
 if (object)
     Instantiate (object, transform.position, transform.rotation);

}

and

backroundreloader.js

var oldobject : Transform; var sightRange = 5.0; var target : Transform; var changeDelay = 0.0;

function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; } // If the distance is less than sightRange if (Vector3.Distance(transform.position, target.position) < sightRange) { Invoke("loadOld", changeDelay); }

function loadOld () { BroadcastMessage ("inSight"); }

function inSight () { // Destroy object Destroy(gameObject);

 // change to old object
 if (oldobject)
     Instantiate (oldobject, transform.position, transform.rotation);

}

Comment
Add comment · Show 1
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 Jessy · Jan 26, 2011 at 02:18 AM 0
Share

Do not post the same thing here and on the forum without cross-linking. http://forum.unity3d.com/threads/75457-Backround-loader?highlight=backround

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Mike 3 · Jan 26, 2011 at 02:31 AM

You're only checking the distance once. What you should do is check periodically (preferrably not every frame)

e.g.:

function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform;

 //check the distance every 0.5s
 InvokeRepeating("CheckDistance", 0.5, 0.5);

}

function CheckDistance() { // If the distance is less than sightRange if (Vector3.Distance(transform.position, target.position) < sightRange) { Invoke("loadOld", changeDelay); } }

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 Given · Jan 26, 2011 at 03:49 AM 0
Share

Well now it doesn't do anything at all

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

No one has followed this question yet.

Related Questions

Access a function of an instantiated prefab 1 Answer

Multiplayer player spawn help 1 Answer

How to instantiate multiple prefabs in multiple positions? 0 Answers

Network.Instantiate players at each spawnpoint? Don't use the same spawnpoint? 1 Answer

Player invisible on respawn 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