- Home /
Show image depending on RaycastHit.
Hey! I'm trying to get a path depending on wich object that is selected (Using raycast). Here's the code that I got so far, maybe it'll explain what I'm trying to do.
var customSkinTwo : GUISkin; var istrue = false; var vag : System.IO.Path; var objektnamn : String;
static var Player1 : String; //Gets filepath+filename from another script.
var theFullPath : String; var thePath : String;
public var www : WWW; function SetImg() { yield; var vag = theFullPath; var www = new WWW (vag); yield www; istrue = true; icon = www.texture; }
function Update () { if ( Input.GetMouseButtonDown(1) ) { var hit : RaycastHit; var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (ray, hit, 1000.0)) { objektnamn = hit.collider.gameObject.name; } }
thePath = this.gameObject.name; theFullPath = "file:///"+thePath;
if (objektnamn == this.gameObject.name) { SetImg(); }
}
function OnGUI () {
if (istrue) {
GUI.skin = customSkinTwo;
GUI.Box (Rect (10,225, 100, 50), icon);
}
}
So, the problem is that theFullPath becomes "file:///Player1" obviously. I want to make it "file:///the_path_that_Player1_var_contains", how to? (Assume that "Player1" is selected using the raycast.)
New code:
function Update () { if ( Input.GetMouseButtonDown(1) ) { var hit : RaycastHit; var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (ray, hit, 1000.0)) { //Debug.Log(hit.collider.gameObject.name); objektnamn = hit.collider.gameObject.name; } }
obj = GameObject.Find(objektnamn);
Debug.Log("file:///" + eval( obj.name )); //<--- Line 41.
theFullPath = "file:///" + eval(obj.name);
if (objektnamn == this.gameObject.name) { SetImg(); } }
Error for new code:
CompilationErrorsException: script(1,9): BCE0005: Boo.Lang.Compiler.CompilerError: Unknown identifier: 'lvl2'.
UnityScript.Scripting.Evaluator.DoCompile () UnityScript.Scripting.Evaluator.CompileScript () UnityScript.Scripting.Evaluator.Run () UnityScript.Scripting.Evaluator.Eval (UnityScript.Scripting.EvaluationContext context, System.String code) PictureShow.Update () (at Assets/Scripts/PictureShow.js:41)
How does Player1 contain this path? Is it part of it's name? Is it stored in a script attached to the GameObject? If it's the latter change thePath = this.gameObject.name to this.gameObject.GetComponent("name_of_script_that_contains_path_and_is_attached_to_player1").path
It's get the path using a filebrowser. The var contains the path.
Also, it's not attached to the same gameobject as the script above. It's a string.
Answer by Santa · May 02, 2011 at 11:18 AM
As far as I understand this should work:
static var Player1 : String = "some_path";
function Update () { var obj : GameObject = GameObject.Find("Player1"); Debug.Log("file:///" + eval( obj.name )); }
It's work, kinda. Iv'e edited it a lil' bit, problem is that, when GameObject.Find("Player1"); is GameObject.Find("Player1-lvl2"); ins$$anonymous$$d, i get the following error:
CompilationErrorsException: script(1,9): BCE0005: Boo.Lang.Compiler.CompilerError: $$anonymous$$ identifier: 'lvl2'.
I'll post the code that i'm using in the question.
You can't use "-" in variable name. So you must name objects only with variable na$$anonymous$$g rules (without spaces, '.', '+', '-' etc).
Answer by efge · May 02, 2011 at 10:52 AM
If your object names and path names are Strings you could use standard String operators(?).
Your answer
Follow this Question
Related Questions
!texture.texture error 0 Answers
Finding size of images imported at runtime 2 Answers
Detect WWW Image bitmap dimensions? 2 Answers
Loading a higher resolution image into Unity than what is supported 0 Answers
How do you download image to UI.Image? 4 Answers