- Home /
How can I enable an object in js?
An instance of type 'UnityEngine.GameObject' is required to access non static member 'SetActive'. This is the code that I am trying to get to work: ship = GameObject.SetActive(true); I am trying to enable an object in js from a script not attached to that object. Is this even possible?
Answer by Sketch_Kidd · May 24, 2015 at 10:34 PM
It is possible try
var yourGameObject = GameObject.Find('your gameobject');
yourGameObject.SetActive(true);
it would be easier to solve if you post an example code, personally I prefer C# but this can be achieved in both unityScript and C#
Answer by Snownebula · May 25, 2015 at 12:10 AM
var target : Transform;
var distance = 3.0;
var xSpeed = 200.0;
var ySpeed = 200.0;
var yMinLimit = -20.0;
var yMaxLimit = 80.0;
var mLimit = 0.0;
public var ship;
public var shipb;
public var ship2 : GameObject;
private var rightclicked = null;
private var x = 0.0;
private var y = 0.0;
var minDistance = 1.0;
var maxDistance = 20000.0;
public var s1 : boolean = false;
public var s2 : boolean = false;
public var s3 : boolean = false;
public var s4 : boolean = false;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
//Debug.Log("Hit 0.");
//CheckActive1();
//Debug.Log("Hit 6.");
// Make the rigid body not change rotation
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function Update () {
CheckActive1();
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.position = position;
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")* mLimit, minDistance, maxDistance);
//distance += -300.0f*Input.GetAxis ("Mouse ScrollWheel")*5, minDistance, maxDistance);
//")*Time.deltaTime;
if(distance < minDistance)
distance = minDistance;
else
if(distance > maxDistance)
distance = maxDistance;
if (Input.GetMouseButtonDown(1)) {
rightclicked = true;
}
if (Input.GetMouseButtonUp(1)) {
rightclicked = false;
}
}
function CheckActive1 () {
Debug.Log("Hit 0.1.");
if (s1 == true) {
//GameObject.FindWithTag("t1").Transform.SetActive;
Debug.Log("Hit 1.");
//ship = GameObject.SetActive(true).FindWithTag("t1");
//Debug.Log("Hit 1.3.");
ship2 = GameObject.FindGameObjectWithTag("t1").SetActive(true);
//var ship2 = GameObject.FindWithTag("t1");
//ship2 = enabled(true);
target = ship2.transform;
//ship = GameObject.FindWithTag("t1").transform;
//Debug.Log("Hit 2.");
//target = ship;
Debug.Log("Hit 3.");
mLimit = 1000;
Debug.Log("Hit 4.");
minDistance = 7500;
Debug.Log("Hit 5.");
distance = 10000;
maxDistance = 20000;
s1 = false;
//}
}
else if (s2 == true) {
//GameObject.FindWithTag("t2").Transform.SetActive;
Debug.Log("Hit 1b.");
ship = GameObject.FindWithTag("t2").enabled = true;
Debug.Log("Hit 1.2b");
ship = GameObject.FindWithTag("t2").transform;
Debug.Log("Hit 2b.");
target = ship;
Debug.Log("Ship: " + ship);
mLimit = 1;
Debug.Log("Mouse Limit: " + mLimit);
distance = 3;
Debug.Log("Camera Distance: " + distance);
minDistance = 2.30;
Debug.Log("Min Distance: " + minDistance);
maxDistance = 60;
Debug.Log("Max Distance: " + maxDistance);
s2 = false;
//}
}
}
function LateUpdate () {
if (target && rightclicked == true) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.01;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.01;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Answer by Hexer · May 25, 2015 at 12:55 AM
As for so far I know, You cannot check (enable) your gameObject when it is unchecked (disabled in the hierarchy).
Try a different approach like disabling a component from that gameObject to get the desired effect.
EDIT : As for Sketch_Kidd answer, that only works when you try it the other way. (when you try to disable the gameObject)
Your answer
Follow this Question
Related Questions
PlayerCamera.AudioListener.enabled = false; will not work 1 Answer
physics.OverlapSphere colliders 1 Answer
sync renderer.enabled or SetActive() over network? 0 Answers
sync renderer.enabled or SetActive() over network? 2 Answers
works with gameObject but not with Image SetActive vs Enabled 1 Answer