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 Cobradabest · Jan 05, 2013 at 06:15 PM · c#referencereferencingbooleans

I'm having problems with referencing other scripts.

I'm having a bit of trouble referencing a script in my game.

What I have is a projectile that should shoot out of the gun, but thescript that contains the bool I'm using to declare whether or not the gun is shooting is in the gun itself. So I decided to try and reference it from the launcher script

It can't get it to work, though, here's the code as it is right now:

 using UnityEngine;
 using System.Collections;
 
 public class Launcher : MonoBehaviour {
     
     public Rigidbody projectile; //Variable to hold our projectile object of type Rigidbody.
     public short speed = 999; //Variable 'speed' which determines how fast the projectile will shoot.
     public RayShoot gun1Script = transform.parent.gameObject.GetComponent<RayShoot>();
     
     void  Start ()
     {
         collisionParticle.emit = false;
     }
     void  Update ()
     {
         if (gun1Script.shooting) //If player clicks left mouse button.
         {
             Rigidbody instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
             instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
             Destroy(instantiatedProjectile.gameObject, 10);
             //print("IT WORKS!!!");
             //Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
         }
     }
 }

I'm getting this error message:

Assets/Tutorial Stuff/Scripts/C#/Launcher.cs(8,38): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'

What do I do?

Comment
Add comment · Show 1
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 Cobradabest · Jan 05, 2013 at 08:57 PM 0
Share

I noticed I made a mistake, the object that had the script, wasn't the parent, but IT'S parent, so I set it it public, and in Unity itself, I just dragged the object with the script into the slot in the Inspector, and it did the trick! Something tells me this isn't the right way of doing it, but it works just fine!

2 Replies

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

Answer by Eric5h5 · Jan 05, 2013 at 06:24 PM

You should only declare variables outside functions. Actual code that initializes variables should always go inside functions, in this case Awake or Start.

 private RayShoot gun1Script;

 void Awake () {
     gun1Script = transform.parent.GetComponent<RayShoot>();
 }

By the way, you don't need TransformDirection here, you can just do

 instantiatedProjectile.velocity = transform.forward * speed;
Comment
Add comment · 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
0

Answer by RazorCut · Jan 05, 2013 at 06:49 PM

You cannot initialize gun1Script member during object construction. You have to do it in Awake(), Start(), or later. In your case, Awake() will fix it for you.

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

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

10 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

Related Questions

How do you reference a GameObject in a C# script that is a part of that GameObject? 1 Answer

C# utilities functions in other script 1 Answer

How do I check if an Object isn't moving? (C#) 1 Answer

Change value of integer based on string input in a separate script 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


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