Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by JakeReis1 · Apr 12, 2016 at 07:52 PM · scripting problemvariablesscript errorsemicolon

Changing a variable from another script

Hello! I have this script for an enemy ai -

 var target : Transform; //the enemy's target
 static var moveSpeed : int = 100; //move speed
 var rotationSpeed = 3; //speed of turning
 
 
 var myTransform : Transform; //current transform data of this enemy
 
 
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
 }
 
 
 function Start()
 {
     target = GameObject.FindWithTag("Player").transform; //target the player
 
 
 }
 
 
 function Update () {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
 
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
 }

I'm trying to change the variable moveSpeed from another script. As you can see I've set it to be a static var, so I should be able to access it. This is the other script -

 #pragma strict
 
 function Update() {
 
  
     var hit: RaycastHit; var forward = transform.TransformDirection (Vector3.forward); 
     var Cube : GameObject.Find("Cube");
     
     if (Physics.Raycast (transform.position, forward, hit))
 
         if(hit.collider.CompareTag("enemy")){
 
  
             Debug.Log("Enemy Spotted!");
             Cube.GetComponent.enemyai.moveSpeed = 10;
              
         }else{
 
  
             // Something blocking line of sight
 
 
  
         }
 
  
     Debug.DrawLine (transform.position, hit.point);
 
  
 } 

It's just a script that triggers the action (change move speed to 10) when the player looks at the enemy. However, I am getting the error "';' expected. Insert a semicolon at the end." even though you can clearly see the semicolon. The error is on line 7 which is -

  var Cube : GameObject.Find("Cube");

I have been looking for hours now to try and solve this. Maybe I need to do a different method all together, but I would really appreciate some help. Thank you so much! :)

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

Answer by Redwolve · Apr 12, 2016 at 10:22 PM

@JakeReis1

The way I have started to handle this may be a bit different than most but hopefully it helps you out. Whenever I want anything other than the script a variable is a part of to have access to that variable I will always create a Blank Animator to use as sort of a Data Storage, usually I name it DataCanvas and dataAnimator. And I create all the variables I need for the game/program, that will be accessed throughout my game in there. Works for every number based variable and bool. and this way you just uses dataAnimator.GetInteger("Speed") or dataAnimator.SetInteger("Speed", 2)

Hopefully you can use that. like I said works for me all the time, easier than referencing script and this way everything can change it from the same spot and you can even change the default from the Inspector. Let me know if this helps.

Comment
Add comment · Show 3 · 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 JakeReis1 · Apr 13, 2016 at 03:25 AM 0
Share

Thank you so much you are awesome it works perfectly!

avatar image Redwolve JakeReis1 · Apr 13, 2016 at 03:29 AM 0
Share

No problem glad i could help

avatar image Redwolve JakeReis1 · Apr 13, 2016 at 05:06 PM 0
Share

any chance you could select my answer as the answer, trying to build my reputation up so it would help.

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

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The referenced script on this Behaviour is missing - on any new script 0 Answers

AddListener fuction throwing a NullReferenceException error 1 Answer

Hai guys i need help why my scripts not running "i will make button next3dobject and prev3dobject" 1 Answer

why is this code not working 0 Answers

Help with a C# script .SetActive 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