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 IHazABone · Oct 09, 2013 at 01:38 AM · javascriptanother script

Changing a value in a script but only for a specific object with that script attached?

Currently, I have a script that checks if the player has moved past a certain point, and if so, it changes a boolean in another script attache to a separate object that makes it move. But, how can I reference the script or variable within the script only for that object (as many objects will have this script)?

Trigger code (Ignore the callSend var, does nothing thus far) #pragma strict

 function Start () {
 
 }
 
 var obj : GameObject;
 var callSend = 1;
 
 function Update () {
     var lineRenderer : LineRenderer = GetComponent(LineRenderer);
     
     lineRenderer.useWorldSpace = false;
     lineRenderer.SetVertexCount(2);
     
     var hit : RaycastHit;
     
     Physics.Raycast(transform.position, transform.forward, hit);
     
     if(hit.collider){
         lineRenderer.SetPosition(1,Vector3(0, 0, hit.distance));
         if(hit.collider.tag == "Player") {
             obj.GetComponent(TransformObject).condition = true; //The (not working) line I need.
             Debug.Log("Hit laser. ");
         }
     }
     else{
         lineRenderer.SetPosition(1,Vector3(0, 0, 5000));
     }
     
 }
  
 @script RequireComponent(LineRenderer)

TransformObject.js (Ignore CallRecieve)
#pragma strict

 var time = 5; //In frames.
 var speed = 1; //Time.deltaTimer * this
 var direction = 1; //1 = up, 2 = down, 3 = forward, 4 = backward, 5 = right, 6 = left
 var callRecieve = 1;
 public static var condition = false;
 
 function Start () {
     
 }
 
 function Update () {
     if(condition)MoveStuff();
 }
 
 function MoveStuff() {
     if(direction == 1) {
         if(time > 0) {
                 transform.Translate(Vector3.up * (Time.deltaTime * speed));
                 time -= 1;
                 Debug.Log("Moved up. ");
             }
             } else if(direction == 2) {
         if(time > 0) {
                 transform.Translate(Vector3.down * (Time.deltaTime * speed));
                 time -= 1;
                 Debug.Log("Moved down. ");
             }
         } else if(direction == 3) {
             if(time > 0) {
                 transform.Translate(Vector3.forward * (Time.deltaTime * speed));
                 time -= 1;
                 Debug.Log("Moved forward. ");
             }
         } else if(direction == 4) {
             if(time > 0) {
                 transform.Translate(Vector3.back * (Time.deltaTime * speed));
                 time -= 1;
                 Debug.Log("Moved back. ");
             }
         } else if(direction == 5) {
             if(time > 0) {
                 transform.Translate(Vector3.right * (Time.deltaTime * speed));
                 time -= 1;
                 Debug.Log("Moved right. ");
             }
         } else if(direction == 6) {
             if(time > 0) {
                 transform.Translate(Vector3.left * (Time.deltaTime * speed));
                 time -= 1;
                 Debug.Log("Moved left. ");
             }
         }
 }
Comment
Add comment · Show 9
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 Mr-Knieves · Oct 09, 2013 at 05:18 PM 0
Share

If there a way to differentiate that gameobject from the rest?

You could do a: $$anonymous$$yScript script ($$anonymous$$yScript)GameObject.Find("theobjectiwant")

But it would be easier to keep a reference of that object within the script/object that is deter$$anonymous$$ing that the player moved as .Find is kind of expensive to use.

avatar image IHazABone Mr-Knieves · Oct 09, 2013 at 10:28 PM 0
Share

If I correctly interpreted what your last statement meant, that would be very inadvisable - see, would that not require hundreds of the same script with different references?

avatar image Mr-Knieves Mr-Knieves · Oct 09, 2013 at 10:31 PM 0
Share

Depends on your setup, some example code of what you've done could be helpful.

avatar image IHazABone Mr-Knieves · Oct 09, 2013 at 11:40 PM 0
Share

Updated with code.

avatar image Mr-Knieves · Oct 10, 2013 at 02:37 AM 0
Share

How does "obj" relates to the player or the collider? You may need a list of "obj's" on the collider that you intend to modify, that way you can have something like:

 if(hit.collider)
 {
         lineRenderer.SetPosition(1,Vector3(0, 0, hit.distance));
         if(hit.collider.tag == "Player") {
            $$anonymous$$yScript s = ($$anonymous$$yScript)hit.collider.gameObject.GetComponent<$$anonymous$$yScript>();
            s.obj[0].condition = true;
            
            Debug.Log("Hit laser. ");
 }
avatar image IHazABone · Oct 10, 2013 at 02:41 AM 0
Share

And $$anonymous$$yScript is TransformObject or the trigger script, in my case? Or is it just $$anonymous$$yScript?

avatar image Mr-Knieves · Oct 10, 2013 at 02:45 AM 0
Share

The obj script that contains the TransformObject

avatar image vexe · Oct 10, 2013 at 02:52 AM 2
Share

Not that it's related, but your code has tons of redundant checks, could be reduced to this:

 function $$anonymous$$oveStuff()
 {
     if (time > 0)
     {
         switch(direction)
         {
             case 1: 
                 transform.Translate(Vector3.up * ...);
                 log("moved up");
                 break;
             case 2:
                 transform.Translate(Vector3.down * ...);
                 log("moved down");
                 break;
             case etc
         }
         time -= 1;
     }
 }
Show more comments

1 Reply

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

Answer by clunk47 · Oct 10, 2013 at 02:29 AM

This is pure EXAMPLE. Say you have a script attached to each desired GameObject, named TestScript. Say you have a bool in TestScript called On, which is false by default. Now you want to enable On variable in the first object that contains this script.

 //TestScript.cs
 
 using UnityEngine;
 using System.Collections;
 
 public class TestScript : MonoBehaviour
 {
     public bool On = false;
 }

And now your Example script.

 //Example.CS
     
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Example : MonoBehaviour 
 {
     List<TestScript> test = new List<TestScript>();
     
     void Awake()
     {
         foreach(TestScript t in GameObject.FindObjectsOfType(typeof(TestScript)))
         {
             test.Add(t);
         }
         test[0].On = true;
         print (test[0].On);
     }
 }
     
 
 
 
 
Comment
Add comment · Show 30 · 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 clunk47 · Oct 10, 2013 at 02:49 AM 1
Share

That's why I'm using index test[0]... This changes On bool to true only on the FIRST object in the list. I use the FindObjectsOfType statement to create the list, that's all.

avatar image clunk47 · Oct 10, 2013 at 03:06 AM 1
Share
 //JS version
 
 #pragma strict
 
 import System.Collections.Generic;
 
 var test : List.<TestScript> = new List.<TestScript>();
     
 function Awake()
 {
     for(var t : TestScript in GameObject.FindObjectsOfType(TestScript))
     {
         test.Add(t);
     }
     test[0].On = true;
     print(test[0].On);
 }
avatar image vexe · Oct 10, 2013 at 04:07 AM 1
Share

Of course, why are you wondering why?

 public static var condition = false;

It's a static variable, meaning it's shared between ALL instances of your class. Please see this link to know where, why and why not to use a static variable (Jump to the good coding habits).

If you want each of your objects to have the condition variable, then declare it normally:

 var condition : boolean = false; // no need for the public keyword cause in JS everything is public by default

Statics are not very friendly to new-comers, please avoid it if you're not 100% sure of what you're doing.

avatar image vexe · Oct 10, 2013 at 04:20 AM 1
Share

Not sure if I got you, could you elaborate more? - You mean you want a way to know where your object is sitting inside an array, from the object itself? like myObj.index?

avatar image vexe · Oct 10, 2013 at 04:35 AM 1
Share

That's simple, just add an int called index in your TestScript script. And then when you're adding them to the array, just set the index accordingly. Like (A slight modification of @clunk47's script, no need for a list):

 TestScript[] allObjs = GameObject.FindObjectsOfType(typeof(TestScript)); // as you should know by now, this picks up all your TestScript from your scene in a non-consistent way/order

 for(int i = 0, len = allObjs.Length; i < len; i++)
    allObjs[i].index = i;

Now, your objects store their position in the array. so if you do:

 var obj1 = allObjs[5];
 var obj2 = allObjs[45];

obj1.index is 5, and obj2.index is 45

Show more comments

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

18 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

Related Questions

Accessing variable in another script? 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Removing segments 1 Answer

JS Setting int value from another script 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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