- Home /
Enabling script
i want a script to be disabled until a target is selected heres the script that selects a target
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Movement : MonoBehaviour {
public Transform selectedTarget;
void Update(){
if (Input.GetMouseButtonUp(1)){ // when button clicked...
RaycastHit hit; // cast a ray from mouse pointer:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if enemy hit...
if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Enemy")){
selectedTarget = hit.transform; // set the new one...
SelectTarget(); // and select it
}
}
}
private void SelectTarget(){
Movement2 m = (Movement2)GetComponent("Movement2");
m.target = selectedTarget.transform;
}
}
the script i want disabled is just a script that makes the heroes move to the target and when this script selects that target the other script becomes enabled and moves to the target how would i go about doing this. Thanks in advance
Answer by MC HALO · Aug 26, 2012 at 10:54 PM
You can use something like this, if the target is selected then get the component that has the script you want to disable. To do so use the following once the target is selected
GameObject.Find(" put the name ofthe object that has the script").GetComponent("the script name").enabled = false;
thanks what i was trying to ask was that i want a script to become enabled when a certain object was selected. would the script you posted do that?
it can do :) if you still have problems add me on skype i can help Hummad.Nazir
thanks ill code it right now also ill add you on skype becuase im probably going to have more questions :P
hey again when i used your script it said it could find the object heres what i coded.
using UnityEngine;
using System.Collections;
public class Enabler : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject.Find(Cube).GetComponent($$anonymous$$ovement2).enabled = false;
} }
did i do anything wrong
Answer by Panik.Studios · Aug 26, 2012 at 11:52 PM
That seems a bitmore of beating around the bush.. just make an if statment that says something like
if (target selected = "thing you want"){
do something;
}
this way the script is running but nothing will happen until u select the target..
oh okay so would this work
if(target selected = GetComponent().enabled = true){ do something; }
did i code this right
hey again when i used your code unity said it couldnt find selected what did it do wrong heres what i coded
using UnityEngine;
using System.Collections;
public class Enabler : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
if (target selected = GetComponent().enabled =
false){
do something; }
} }
Oh I'm sorry that was pseudo code not an actual code you copy and paste.. I was just giving you an Idea of what you could do.. I use java script myself so I don't know the exact language for c# but basically I was saying
If your code can select objects... there should be a way of saying.. if _ is the selected object...
I assumed you knew ray casting since you wrote that code.. I however do not understand ray casting.. I also write all my code in unityscript, so I don't
in your case would be the enemy ,,,
so you need to first define what enemy is.. maybe
var enemy = GameObject.FindGameObjectWithTag("enemy");
you also need to define what selected means
if (selected = enemy){
//your code for moving you hero to the enemy goes here
......
I dont know how you would define selected..
raycast hit = selected.. maybe something like that.. as I said I dont understand raycastin since Im very new to this also...
$$anonymous$$yppint is that you dont need to enable or disable scripts.. just make an if statement and if there is nothing selected.. nothing will do anything... Ya Dig?
Your answer
Follow this Question
Related Questions
Mouse click to select and Isolate 0 Answers
enable/disable image effects in code 1 Answer
Raycasting a specific square area 1 Answer