- Home /
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. ");
}
}
}
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.
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?
Depends on your setup, some example code of what you've done could be helpful.
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. ");
}
And $$anonymous$$yScript
is TransformObject or the trigger script, in my case? Or is it just $$anonymous$$yScript
?
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;
}
}
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);
}
}
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.
//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);
}
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.
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
?
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613115557im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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