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 angad singh · Oct 01, 2010 at 11:31 AM · variablesrubikscube

change variables in another object's script's instance

i have these ray cast 'checker' cubes, called CubeBouncerJH and CubeBouncerKU, that check if they are surrounded by other cubes tagged 'Rcube', i implemented this by the following code:-

if(Input.GetKey("k")) { var hitK: RaycastHit;

             //check if we're colliding
             if(Physics.Raycast(transform.position, transform.right, hitK, 1))
             {
                 //...with an object
                 if(hitK.collider.gameObject.tag =="Rcube")
                 {
                 hitK.collider.gameObject.transform.position.y=15;
                 }
             }
     }

now, for testing purposes, i used that 'transform.position.y=15' line to just bump the cube getting hit by the ray up to a different height, to check that im being able to affect properties of the cubes surrounding the checker cubes.

this is code in the 'update' function of the 'checker' cubes' script (they sit back to back so i can check in all 4 directions, eventually I'll be putting up 36 small cubes at 10 degree intervals to create an unavoidable circle of raycasting checkers.

this code worked perfectly, now, to rotate the cubes, im using another script, attached to each and every one of the 'rotatable' cubes surrounding the 'checkers'

at this point, i'd like to point out that the purpose of this code is to eventually facilitate rotation of individual slices of a rubik's cube sort of structure, and these checker cubes are to be invisible 'Slice confirmers' that stick around on the inside of the cube

the script attached to the rotatable cubes is called 'rotationScriptKeyLess.js':-

var willRotateHorizontallyNow: boolean; var willRotateVerticallyNow: boolean; var RotAxis : Vector3; var MyAngle = 0; var to : Transform;

var rotspeed = 0;

var CenterOfRotation;

var anglecounter=0;

function Update () { if ( Input.GetKey("a") && willRotateHorizontallyNow == true) { if(MyAngle ==0) { RotAxis = Vector3.up; MyAngle=90; CenterOfRotation = GameObject.Find ("CubeBouncerJH"); rotspeed = 5; } }

 if ( Input.GetKey("d") && willRotateHorizontallyNow == true)
 {
     if(MyAngle ==0)
     {
         RotAxis = Vector3.up;
         MyAngle=90;
         CenterOfRotation = GameObject.Find ("CubeBouncerJH");
         rotspeed = -5;
     }
 }

if(anglecounter<MyAngle) { transform.RotateAround (CenterOfRotation.transform.position, RotAxis ,rotspeed);

     anglecounter=anglecounter+Mathf.Abs (rotspeed);
 }
 else
     MyAngle=0;

 if(MyAngle == 0)
     anglecounter=0;

}

this code works too, if i choose the cubes individually, and 'check' their 'willRotateHorizontallyNow' checkboxes to manually set the variables for their instance of the script to '1' in the inspector.

what i need, is for the value of their instance to get changed by the raycaster i made, the last bit of code i tried for the same was:-

if(Physics.Raycast(transform.position, transform.right, hitK, 1))
                    {
                        //...with an object
                        if(hitK.collider.gameObject.tag =="Rcube")
                        {
                    hitK.collider.gameObject.rotationScriptKeyLess.willRotateHorizontallyNow=true;
                        }
                    }

and well, it didn't work...

i know that the raycast is correctly hitting the cube, i know that the cube will do exactly what i want it to do once it's boolean value is changed, i just don't know how to change that value as a result of the raycast.

now, hopefully this is all clear enough, i need a solution, or a suggestion, anything! to make this code better, and more importantly, work) any ideas?

thank you :)

Comment
Add comment
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

1 Reply

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

Answer by · Oct 01, 2010 at 11:35 AM

To make it work, it seems like you're just missing GetComponent.

hitK.collider.gameObject.GetComponent(rotationScriptKeyLess).willRotateHorizontallyNow=true;

I hope this solves your problem!

Taking a quick scan across the rest of your code, the only thing I'd suggest is to explicitly declare the type of all of your variables. You've done it for some, but you could safely do it for the rest. (e.g. var rotspeed : int = 0;)

Comment
Add comment · Show 7 · 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 angad singh · Oct 03, 2010 at 06:00 AM 0
Share

i have dabbled with getcomponent a LOT, what i've realized is (correct me if im wrong) you might be able to change the value of variables in a script attached to the same object with getcomponent, but it just doesn't work the same way for another object's scripts, any other suggestions?

i used your code, i've used it before too, didn't work :-\

avatar image angad singh · Oct 03, 2010 at 06:13 AM 0
Share

actually, i just got an idea, i'll just make flag script in the checker box and ask the rotatable cubes to check that variable, let's see :) thanks!

avatar image · Oct 03, 2010 at 11:50 PM 0
Share

GetComponent works fine to get the component (script) on the specified gameObject. hit$$anonymous$$.collider.gameObject specifies which object you should be looking for the scripts in - in this case, the one that was hit by the raycast - and will find the component (script) on that.

avatar image angad singh · Oct 04, 2010 at 06:05 AM 0
Share

it still didn't work for me, wait, is there anything wrong with the placement of the code? my way of debugging is selecting the object before hitting 'play' and then observing the values change in the inspector...is the variable type (public var Vname : boolean) ok?

i'd used a 'static' variable for one program earlier and it worked, but, im not sure if it's the right thing to do here!!

avatar image · Oct 04, 2010 at 12:39 PM 0
Share

A static var will be unique project-wide, so it wouldn't work for your situation. It shouldn't matter where you place that code - as long as the raycast is correctly hitting the gameObject, and that gameObject is tagged "Rcube" and the GetComponent value is exactly the same as the script name, it should all just work. Is there somewhere you could upload the project so I can test it locally?

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

No one has followed this question yet.

Related Questions

Set variable into object and conferring but object Math game 0 Answers

accesing variable from other script 4 Answers

How can I save scripted variable changes? 2 Answers

How do I create a bullet whose velocity and direction is affected by the velocity and direction of its source? 1 Answer

Changing a variable in another script over the network 1 Answer


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